Created
April 6, 2017 15:31
-
-
Save think2011/8dc82425fbbfb9de465b5b95a93ef267 to your computer and use it in GitHub Desktop.
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
var guid = 0; | |
function run() { | |
guid++; | |
var id = guid; | |
return new Promise(resolve => { | |
// resolve in a random amount of time | |
setTimeout(function () { | |
console.log(id); | |
resolve(id); | |
}, (Math.random() * 1.5 | 0) * 1000); | |
}); | |
} | |
var promises = Array.from({ length: 10 }).reduce(function (acc) { | |
return acc.then(function (res) { | |
return run().then(function (result) { | |
res.push(result); | |
return res; | |
}); | |
}); | |
}, Promise.resolve([])); | |
promises.then(console.log); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment