Created
January 21, 2016 06:14
-
-
Save DuaneNielsen/b9520378282dd2144da5 to your computer and use it in GitHub Desktop.
Serial Execution 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
// 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()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
From http://book.mixu.net/node/ch7.html