Guest User

Untitled

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