Created
January 21, 2016 06:15
-
-
Save DuaneNielsen/fa69fdf982ea3bd0d08b to your computer and use it in GitHub Desktop.
Barrier Sync in javascript
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 async(arg, callback) { | |
console.log('do something with \''+arg+'\', return 1 sec later'); | |
setTimeout(function() { callback(arg * 2); }, 1000); | |
} | |
function final() { console.log('Done', results); } | |
var items = [ 1, 2, 3, 4, 5, 6 ]; | |
var results = []; | |
items.forEach(function(item) { | |
async(item, function(result){ | |
results.push(result); | |
if(results.length == items.length) { | |
final(); | |
} | |
}) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://book.mixu.net/node/ch7.html