To run promises concurrently:
var promise1 = promiseAPI(params1);
var promise2 = promiseAPI(params2);
var promise1and2 = $q.all([promise1, promise2]);
To run promises in sequence, send back the next promise to the first promise success handler:
var promise1 = promiseAPI(params1);
var promise1then2 = promise1.then(function() {
var promise2 = promiseAPI(params2);
//return for chaining
return promise2;
});
By using the .then
method of a promise, it is straightforward to create a chain of promises. This allows for chains of varying lengths and since a promise can be resolved with another promise (which will delay its resolution further), you can pause/defer resolution of the promises at any point in the chain.
-- AngularJS $q Service API Reference -- Chaining Promises.