Reputation: 15378
I need to wait for a promise resolution.
function myFunction(myService, projectId) {
var localdata;
var myDataPromise = myService.getData(projectId);
myDataPromise.then(function(result) {
localdata = result;
});
var someResult = someProcess(localdata); // I need to wait for promise resolution.
return someResult;
}
UPDATE
I try to clarify my question. I have the myService service with function which return promise:
var getData = function (projectId) {
return projectResource.getProjectUser({ projectId: projectId }, function (result) {
return result;
}).$promise;
};
Upvotes: 0
Views: 198
Reputation: 22171
You don't need a local variable ...
function myFunction(myService) {
return myService.getData().then(function(result) {
return result;
});
}
and your caller would be:
myFunction(myService).then(function(result){
//you can be sure that result is fully computed here
console.log("Your result " + result);
})
Upvotes: 1
Reputation: 29836
You don't want to return data from a promise!
You do want to return a promise in order to "continue" the chain.
The wait you are looking for is the callbacks you attach to the promise.
function myFunction($scope, myService) {
var myDataPromise = myService.getData();
myDataPromise.then(function(result) {
// this is the "wait" your looking for..
$scope.data = result;
console.log("data.name"+$scope.data.name);
});
return myDataPromise; // return the promise
}
Upvotes: 0