The short answer is no, you do not need an additional library. Promise.then () is fairly atomic. Long answer: it is worth making the queue () function to save the DRY code. Bluebird-promises seems pretty complete, but something here is based on AngularJS $ q.
If I were doing .queue (), I would like it to handle errors.
Here's the angular factory service and some use cases:
/** * Simple promise factory */ angular.module('app').factory('P', function($q) { var P = $q; // Make a promise P.then = function(obj) { return $q.when(obj); }; // Take a promise. Queue 'action'. On 'action' faulure, run 'error' and continue. P.queue = function(promise, action, error) { return promise.then(action).catch(error); }; // Oook! Monkey patch .queue() onto a $q promise. P.startQueue = function(obj) { var promise = $q.when(obj); promise.queue = function(action, error) { return promise.then(action).catch(error); }; return promise; }; return P; });
How to use it:
.run(function($state, YouReallyNeedJustQorP, $q, P) { // Use a $q promise. Queue actions with P // Make a regular old promise var myPromise = $q.when('plain old promise'); // use P to queue an action on myPromise P.queue(myPromise, function() { return console.log('myPromise: do something clever'); }); // use P to queue an action P.queue(myPromise, function() { throw console.log('myPromise: do something dangerous'); }, function() { return console.log('myPromise: risks must be taken!'); }); // use P to queue an action P.queue(myPromise, function() { return console.log('myPromise: never quit'); }); // Same thing, but make a special promise with P var myQueue = P.startQueue(myPromise); // use P to queue an action myQueue.queue(function() { return console.log('myQueue: do something clever'); }); // use P to queue an action myQueue.queue(function() { throw console.log('myQueue: do something hard'); }, function() { return console.log('myQueue: hard is interesting!'); }); // use P to queue an action myQueue.queue(function() { return console.log('myQueue: no easy days'); });
source share