Created
May 10, 2022 20:51
-
-
Save mpuz/34b4a1ce73f9d8fe7b0b776662ec270a to your computer and use it in GitHub Desktop.
Promisify for of loo[
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
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