AngularJS : $q -> deferred API order of things (lifecycle) AND who invokes digest?

Promises have three states Pending – this is how promises start. Fulfilled – this is what happens when you resolve a deferred, or when the return value from .then fulfills, and it generally analogous to a standard return value. Rejected – This is what happens when you reject a deferred, when you throw from a … Read more

How can I access the value of a promise?

promiseA‘s then function returns a new promise (promiseB) that is immediately resolved after promiseA is resolved, its value is the value of the what is returned from the success function within promiseA. In this case promiseA is resolved with a value – result and then immediately resolves promiseB with the value of result + 1. … Read more

angular $q, How to chain multiple promises within and after a for-loop

What you need to use is $q.all which combines a number of promises into one which is only resolved when all the promises are resolved. In your case you could do something like: function outerFunction() { var defer = $q.defer(); var promises = []; function lastTask(){ writeSome(‘finish’).then( function(){ defer.resolve(); }); } angular.forEach( $scope.testArray, function(value){ promises.push(writeSome(value)); … Read more

Wait for all promises to resolve

I want the all to resolve when all the chains have been resolved. Sure, then just pass the promise of each chain into the all() instead of the initial promises: $q.all([one.promise, two.promise, three.promise]).then(function() { console.log(“ALL INITIAL PROMISES RESOLVED”); }); var onechain = one.promise.then(success).then(success), twochain = two.promise.then(success), threechain = three.promise.then(success).then(success).then(success); $q.all([onechain, twochain, threechain]).then(function() { console.log(“ALL PROMISES … Read more

How to use $http promise response outside success handler

But if I want to use $scope.tempObject after callback so how can I use it. ? You need to chain from the httpPromise. Save the httpPromise and return the value to the onFullfilled handler function. //save httpPromise for chaining var httpPromise = $http({ method: ‘GET’, url: ‘/myRestUrl’ }).then(function onFulfilledHandler(response) { $scope.tempObject = response console.log(“Temp Object … Read more

Does never resolved promise cause memory leak?

Well, I’m assuming you don’t keep an explicit reference to it since that would force it to stay allocated. The simplest test I could think of is actually allocating a lot of promises and not resolving them: var $q = angular.injector([“ng”]).get(“$q”); setInterval(function () { for (var i = 0; i < 100; i++) { var … Read more

Caching a promise object in AngularJS service

Is this the right approach? Yes. The use of memoisation on functions that return promises a common technique to avoid the repeated execution of asynchronous (and usually expensive) tasks. The promise makes the caching easy because one does not need to distinguish between ongoing and finished operations, they’re both represented as (the same) promise for … Read more