Skip to content

Instantly share code, notes, and snippets.

@laispace
Created February 2, 2016 09:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save laispace/7e49003a1457a8884840 to your computer and use it in GitHub Desktop.
Save laispace/7e49003a1457a8884840 to your computer and use it in GitHub Desktop.
invoke a callback after all operations
function after (count, callback) {
return function () {
while(--count === 0) {
console.log(arguments);
callback.call(this, arguments);
}
}
}
// define some operations
function func1 () {console.log('func1 done.'); done(1);}
function func2 () {console.log('func2 done.'); done(2);}
function func3 () {console.log('func3 done.'); done(3);}
var funcs = [func1, func2, func3];
var done = after(funcs.length, function () {
console.log('all things done!', arguments);
});
// test
funcs.forEach(function (func) {
func();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment