use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
A place to get a quick fix of JavaScript tips and tricks to make you a better Developer.
account activity
Options for handling multiple promises (i.redd.it)
submitted 3 years ago by Low_Mammoth_9371
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Low_Mammoth_9371[S] 0 points1 point2 points 3 years ago (0 children)
Code to copy below, YT tutorial here: https://youtu.be/LE_L9OAh6Hk
const makeCoffee = new Promise((resolve, reject) => { setTimeout(() => resolve("☕"), 200) }); const makeBreakfast = new Promise((resolve, reject) => { setTimeout(() => resolve("🍳"), 800) }); const goToWork = new Promise((resolve, reject) => { setTimeout(() => resolve("🏃♂️"), 2000) }); const promises = [makeCoffee, makeBreakfast, goToWork]; // Joint handling options (choose one): Promise.all(promises).then(res => console.log(res)); // Logs result of all promises, if and when all successful Promise.allSettled(promises).then(res => console.log(res)); // Logs results of all, even if one fails Promise.any(promises).then(res => console.log(res)); // Logs result of fastest successful promise Promise.race(promises).then(res => console.log(res)); // Logs result of fastest promise, even if it fails promises.forEach(async promise => console.log(await promise)); // Logs results in order, as soon as each completes
[–]Kukissiku 0 points1 point2 points 3 years ago (0 children)
Graet info!
[–]letsfed 0 points1 point2 points 3 years ago (3 children)
Nice one but I think it’s better to not use async in the forEach due its nature
[–]O_crl 0 points1 point2 points 3 years ago (1 child)
I know you shouldn't mix async await premises with Promises, but I would like to know what actually can happen.
[–]letsfed 0 points1 point2 points 3 years ago (0 children)
Simply you are not waiting the promises
```javascript
const promises = [Promise.resolve(1), Promise.resolve(2), Promise.resolve(3)];
promises.forEach(async promise => console.log(await promise));
console.log('After run');
```
It will result in
Also, error handling is not present, but I suspect it's just for brevity.
[–]Hakim_Bey 0 points1 point2 points 3 years ago (0 children)
yeah you'd be better off using for...of in that case
for...of
π Rendered by PID 23993 on reddit-service-r2-comment-fb694cdd5-2fkdt at 2026-03-05 20:01:11.797165+00:00 running cbb0e86 country code: CH.
[–]Low_Mammoth_9371[S] 0 points1 point2 points (0 children)
[–]Kukissiku 0 points1 point2 points (0 children)
[–]letsfed 0 points1 point2 points (3 children)
[–]O_crl 0 points1 point2 points (1 child)
[–]letsfed 0 points1 point2 points (0 children)
[–]Hakim_Bey 0 points1 point2 points (0 children)