Created
          October 21, 2011 13:59 
        
      - 
      
 - 
        
Save getify/1303923 to your computer and use it in GitHub Desktop.  
    possible API for asyncGate.js... what do you think?
  
        
  
    
      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 foo(done) { | |
| setTimeout(done,5000); | |
| } | |
| function bar(done) { | |
| setTimeout(done,1000); | |
| } | |
| function baz() { | |
| alert("all done!"); | |
| } | |
| // this means: execute `foo` and `bar`, wait for both of them to signal completion, then do `baz` | |
| $AG(foo,bar).then(baz); | 
  
    
      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 foo(done) { | |
| setTimeout(done,5000); | |
| } | |
| function bar(done) { | |
| setTimeout(done,1000); | |
| } | |
| function baz() { | |
| alert("all done!"); | |
| } | |
| var gate = $AG(foo); | |
| // later | |
| gate.and(bar); // you can keep adding to the gate as much as you want... | |
| // later | |
| gate.then(baz); // ...until you start adding listeners | |
| // later | |
| gate.then(function(){ // ...and you can have as many listeners for a gate as you want, | |
| // even if it comes after the gate is already open | |
| alert("already done awhile ago!"); | |
| }); | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment