Skip to content

Instantly share code, notes, and snippets.

@nstawski
Created April 20, 2019 19:02
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 nstawski/b84520835ac60005935b23fed74d6064 to your computer and use it in GitHub Desktop.
Save nstawski/b84520835ac60005935b23fed74d6064 to your computer and use it in GitHub Desktop.
Simple promise display in the right order
let promises = [];
console.log("Displaying promises as they resolve, without proper order:")
for (let i = 0; i < 10; i++) {
let myPromise = new Promise((resolve, reject) => {
setTimeout(() => {
console.log(i);
resolve(i);
}, Math.floor(Math.random() * 5000));
});
promises.push(myPromise);
}
Promise.all(promises).then(function(results) {
console.log("Now in order:");
results.map(el => console.log(el));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment