Last active
August 29, 2015 14:23
-
-
Save hiroshi-maybe/8193bb854365052b1e02 to your computer and use it in GitHub Desktop.
Sequential timeouts by promises
This file contains hidden or 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
| // 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