r/JavaScriptTips • u/Low_Mammoth_9371 • Feb 06 '23
Options for handling multiple promises
25
Upvotes
1
1
u/letsfed Feb 06 '23
Nice one but I think itβs better to not use async in the forEach due its nature
1
u/O_crl Feb 06 '23
I know you shouldn't mix async await premises with Promises, but I would like to know what actually can happen.
1
u/letsfed Feb 06 '23
Simply you are not waiting the promises
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
After run
1
2
3
Also, error handling is not present, but I suspect it's just for brevity.
1
1
u/Low_Mammoth_9371 Feb 06 '23
Code to copy below, YT tutorial here: https://youtu.be/LE_L9OAh6Hk