Created
          November 4, 2010 09:02 
        
      - 
      
- 
        Save jakearchibald/662251 to your computer and use it in GitHub Desktop. 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | function asyncQueue(/* function, function, ... */) { | |
| var funcs = Array.prototype.slice.call(arguments, 0), | |
| baseArgs = [function() { | |
| next(arguments); | |
| }], | |
| next = function(args) { | |
| var nextFunc = funcs.shift(); | |
| nextFunc && nextFunc.apply( this, baseArgs.concat(args) ); | |
| }; | |
| next(); | |
| }; | |
| // eg... | |
| asyncQueue(function(next) { | |
| console.log('1'); | |
| setTimeout(next, 1000); | |
| }, function(next) { | |
| console.log('2'); | |
| setTimeout(next, 1000); | |
| }, function(next) { | |
| console.log('3'); | |
| setTimeout(next, 1000); | |
| }, function(next) { | |
| console.log('4'); | |
| setTimeout(next, 1000); | |
| }); | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment