Created
February 11, 2025 15:19
-
-
Save benhatsor/54b1d0ba57c6be2a40c80b4e8b6ead1e to your computer and use it in GitHub Desktop.
Await all promises in array
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
/* | |
// without response: | |
for await (async of promises) {} | |
// with response: | |
for await (const resp of promises) { | |
console.log(resp); // same as: const resp = await promise; | |
} | |
*/ | |
// example: | |
const promise1 = new Promise(resolve => { window.setTimeout(() => { resolve('test') }, 1000); }); | |
const promise2 = new Promise(resolve => { resolve('test2') }); | |
const promises = [promise1, promise2]; | |
(async function () { | |
window.setTimeout(async () => { | |
for await (async of promises) { | |
console.log("promise resolved"); | |
} | |
console.log("all promises resolved!"); | |
}, 2000); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment