Skip to content

Instantly share code, notes, and snippets.

@mpuz
Created May 10, 2022 20:51
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 mpuz/34b4a1ce73f9d8fe7b0b776662ec270a to your computer and use it in GitHub Desktop.
Save mpuz/34b4a1ce73f9d8fe7b0b776662ec270a to your computer and use it in GitHub Desktop.
Promisify for of loo[
function doSomethingAsync(value) {
return new Promise((resolve) => {
setTimeout(() => {
console.log("Resolving " + value);
resolve(value);
}, Math.floor(Math.random() * 1000));
});
}
function test() {
const promises = [];
for (let i = 0; i < 5; ++i) {
promises.push(doSomethingAsync(i));
}
Promise.all(promises)
.then((results) => {
console.log("All done", results);
})
.catch((e) => {
// Handle errors here
});
}
test();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment