Guest User

Untitled

a guest
Jun 20th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.60 KB | None | 0 0
  1. function asyncQueue(/* function, function, ... */) {
  2. var slice = Array.prototype.slice,
  3. funcs = slice.call(arguments, 0),
  4. baseArgs = [function() {
  5. next(arguments);
  6. }],
  7. next = function(args) {
  8. var nextFunc = funcs.shift();
  9. nextFunc && nextFunc.apply(
  10. this, baseArgs.concat( slice.call(args, 0) )
  11. );
  12. };
  13. next();
  14. };
  15.  
  16. // eg...
  17. asyncQueue(function(next) {
  18. console.log('1');
  19. setTimeout(next, 1000);
  20. }, function(next) {
  21. console.log('2');
  22. setTimeout(next, 1000);
  23. }, function(next) {
  24. console.log('3');
  25. setTimeout(next, 1000);
  26. }, function(next) {
  27. console.log('4');
  28. setTimeout(next, 1000);
  29. });
Add Comment
Please, Sign In to add comment