Skip to content

Instantly share code, notes, and snippets.

@DuaneNielsen
Created January 21, 2016 06:14
Show Gist options
  • Save DuaneNielsen/b9520378282dd2144da5 to your computer and use it in GitHub Desktop.
Save DuaneNielsen/b9520378282dd2144da5 to your computer and use it in GitHub Desktop.
Serial Execution in javascript
// Async task (same in all examples in this chapter)
function async(arg, callback) {
console.log('do something with \''+arg+'\', return 1 sec later');
setTimeout(function() { callback(arg * 2); }, 1000);
}
// Final task (same in all the examples)
function final() { console.log('Done', results); }
// A simple async series:
var items = [ 1, 2, 3, 4, 5, 6 ];
var results = [];
function series(item) {
if(item) {
async( item, function(result) {
results.push(result);
return series(items.shift());
});
} else {
return final();
}
}
series(items.shift());
@DuaneNielsen
Copy link
Author

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