Skip to content

Instantly share code, notes, and snippets.

@rdtr
Created August 30, 2015 21:53
Show Gist options
  • Save rdtr/8eb40403fbe91cedfb5f to your computer and use it in GitHub Desktop.
Save rdtr/8eb40403fbe91cedfb5f to your computer and use it in GitHub Desktop.
setTimeout sequeitial
# node --harmony sequential_timeout.js
# need to install co
var processor = function(timer) {
return new Promise(function(resolve, reject) {
setTimeout(resolve, timer * 1000);
});
}
var serial_timeout = function(timer_arr) {
var co = require('co');
co(function *() {
for (var i = 0; i < timer_arr.length; i++) {
yield processor(timer_arr[i]);
console.log("processing... [" + i + "]");
}
console.log("finish!");
});
}
serial_timeout([1, 3, 5, 7, 9, 11, 13, 15]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment