Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- define(["jquery"], function($) {
- //
- // Given a list of functions that return a promise or an object,
- // Set them up to execute them in order using those promises.
- // and returns a promise that resolves after the last promises returned by the function pipeline resolves.
- //
- var deferredPipeline = $.deferredPipeline = function(/* [function, ]*/) {
- //reversed pipeline
- var toPipe = Array.prototype.slice.call(arguments).reverse();
- if (toPipe.length == 0) {
- return $.when();
- } else if (toPipe.length == 1) {
- return toPipe[i]();
- }
- var dfd = $.Deferred();
- var lastFunc = function() {
- toPipe[0](arguments).then(
- function(){dfd.resolve(arguments);},
- function(){dfd.reject(arguments);});
- };
- for (var i = 1;i<toPipe.length;i++) {
- var toResolveNext = lastFunc;
- lastFunc = function() {
- $.when(toPipe[i](arguments)).then(toResolveNext);
- };
- }
- return dfd.promise();
- };
- return deferredPipeline;
- });
Add Comment
Please, Sign In to add comment