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

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

What are the differences between Deferred, Promise and Future in JavaScript?

These answers, including the selected answer, are good for introducing promises conceptually, but lacking in specifics of what exactly the differences are in the terminology that arises when using libraries implementing them (and there are important differences). Since it is still an evolving spec, the answer currently comes from attempting to survey both references (like … Read more

How do I chain three asynchronous calls using jQuery promises?

In each case, return the jqXHR object returned by $.ajax(). These objects are Promise-compatible so can be chained with .then()/.done()/.fail()/.always(). .then() is the one you want in this case, exactly as in the question. function first() { return $.ajax(…); } function second(data, textStatus, jqXHR) { return $.ajax(…); } function third(data, textStatus, jqXHR) { return $.ajax(…); … Read more