Skip to content

Instantly share code, notes, and snippets.

@lpgauth
Created June 16, 2011 14:32
Show Gist options
  • Save lpgauth/1029334 to your computer and use it in GitHub Desktop.
Save lpgauth/1029334 to your computer and use it in GitHub Desktop.
nodejs pattern
var wait = function(callbacks, done) {
var counter = callbacks.length;
var next = function() {
if(--counter == 0) {
done();
}
};
for(var i = 0; i < callbacks.length; i++) {
callbacks[i](next);
}
}
var a = function (next) {
setTimeout( function() {
console.log("Done A");
next();
}, 3000);
};
var b = function (next) {
setTimeout( function() {
console.log("Done B");
next();
}, 2000);
};
var c = function (next) {
setTimeout( function() {
console.log("Done C");
next();
}, 1000);
};
var d = function () {
console.log("All done!");
};
wait([a, b, c], d );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment