Skip to content

Instantly share code, notes, and snippets.

@hiroshi-maybe
Last active August 29, 2015 14:23
Show Gist options
  • Select an option

  • Save hiroshi-maybe/8193bb854365052b1e02 to your computer and use it in GitHub Desktop.

Select an option

Save hiroshi-maybe/8193bb854365052b1e02 to your computer and use it in GitHub Desktop.
Sequential timeouts by promises
// node --harmony timeout-seq.js
[3,5,10]
.map(function(sec) { return sec * 1000; })
.map(function(duration) {
return function task(resolve) {
setTimeout(function() {
console.log(duration);
resolve();
}, duration);
};
})
.reduce(function(prev, task) {
return prev.then(function() { return new Promise(task); });
}, Promise.resolve())
.then(function() {
console.log('done');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment