Skip to content

Instantly share code, notes, and snippets.

@think2011
Created April 6, 2017 15:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save think2011/8dc82425fbbfb9de465b5b95a93ef267 to your computer and use it in GitHub Desktop.
Save think2011/8dc82425fbbfb9de465b5b95a93ef267 to your computer and use it in GitHub Desktop.
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