Skip to content

Instantly share code, notes, and snippets.

@getify
Created October 21, 2011 13:59
Show Gist options
  • Save getify/1303923 to your computer and use it in GitHub Desktop.
Save getify/1303923 to your computer and use it in GitHub Desktop.
possible API for asyncGate.js... what do you think?
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);
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