Created
February 2, 2016 09:49
-
-
Save laispace/7e49003a1457a8884840 to your computer and use it in GitHub Desktop.
invoke a callback after all operations
This file contains 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 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