Skip to content

Instantly share code, notes, and snippets.

@benhatsor
Created February 11, 2025 15:19
Show Gist options
  • Save benhatsor/54b1d0ba57c6be2a40c80b4e8b6ead1e to your computer and use it in GitHub Desktop.
Save benhatsor/54b1d0ba57c6be2a40c80b4e8b6ead1e to your computer and use it in GitHub Desktop.
Await all promises in array
/*
// 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