javascript - getting the return data of a async function inside a function

前端 未结 2 1051
南旧
南旧 2021-01-05 15:57

I am having a problem because chrome api functions are async and I cant get its return value. Consider the following code. I am using angularjs

             


        
2条回答
  •  暖寄归人
    2021-01-05 16:42

    Another way is to use a promise. In this case it might not matter but if you have a a lot of nested callbacks then a promise is better.

    $scope.storageGet = function(param) {
        var deferred = $q.defer();
    
        chrome.storage.local.get(param.storageName, function(data) {
            deferred.resolve(data);
        });
    
        return deferred.promise;
    };
    

    And then you call it like this.

    $scope.storageGet(param).then(function (data) {
    
    });
    

提交回复
热议问题