Skip to content

Instantly share code, notes, and snippets.

@DuaneNielsen
Created January 21, 2016 06:15
Show Gist options
  • Save DuaneNielsen/fa69fdf982ea3bd0d08b to your computer and use it in GitHub Desktop.
Save DuaneNielsen/fa69fdf982ea3bd0d08b to your computer and use it in GitHub Desktop.
Barrier Sync in javascript
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();
}
})
});
@DuaneNielsen
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment