r/JavaScriptTips • u/Low_Mammoth_9371 • Jan 16 '23
Promises can be consumed sequentially by using await in a loop
18
Upvotes
1
u/SuperLeo_0 Jan 16 '23
how about Promise.all()?
1
u/Low_Mammoth_9371 Jan 16 '23
That won't run promises sequentially (one after another). Instead it will start all promises at the same time and return all values if all are successful and throw an error otherwise.
1
1
u/prof3ssorSt3v3 Jan 16 '23
You need to use
for await of loops.
1
u/FancyADrink Mar 01 '24
Is this a thing?
1
u/prof3ssorSt3v3 Mar 04 '24
Yep. Reference on mdn https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for-await...of
My tutorial on the topic https://youtu.be/unDSLi5zBXU
1
u/Low_Mammoth_9371 Jan 16 '23
Code to copy: