Guest User

Untitled

a guest
Aug 17th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. define(["jquery"], function($) {
  2. //
  3. // Given a list of functions that return a promise or an object,
  4. // Set them up to execute them in order using those promises.
  5. // and returns a promise that resolves after the last promises returned by the function pipeline resolves.
  6. //
  7. var deferredPipeline = $.deferredPipeline = function(/* [function, ]*/) {
  8. //reversed pipeline
  9. var toPipe = Array.prototype.slice.call(arguments).reverse();
  10. if (toPipe.length == 0) {
  11. return $.when();
  12. } else if (toPipe.length == 1) {
  13. return toPipe[i]();
  14. }
  15. var dfd = $.Deferred();
  16. var lastFunc = function() {
  17. toPipe[0](arguments).then(
  18. function(){dfd.resolve(arguments);},
  19. function(){dfd.reject(arguments);});
  20. };
  21. for (var i = 1;i<toPipe.length;i++) {
  22. var toResolveNext = lastFunc;
  23. lastFunc = function() {
  24. $.when(toPipe[i](arguments)).then(toResolveNext);
  25. };
  26. }
  27. return dfd.promise();
  28. };
  29. return deferredPipeline;
  30. });
Add Comment
Please, Sign In to add comment