Re:Ask HN: What are some cool but obscure data structures yo...
by mromnia on Friday July 22, 2022 @06:45AM
(#32190557)
Iterating with async/await means you wait for every result before making another call. You basically execute the async calls one by one.
Promise.all runs them all simultaneously and waits until they are all complete. It returns an array of results in the same order as the calls, so it's usually pretty straightforward to use.
Both approaches have a purpose, so it's not like you "should" strictly use one. But you should be aware of both and be able to use the one that fits the need.
Re:Ask HN: What are some cool but obscure data structures yo...
by dmix on Friday July 22, 2022 @08:31AM
(#32191232)
The third major strategy being Promise.any():
- as soon as any of them resolve (any)
- when all of them resolve (all)
- resolve each in order (for)
Re:Ask HN: What are some cool but obscure data structures yo...
by seedboot on Friday July 22, 2022 @06:41AM
(#32190534)
Promise.all/allSettled/etc will allow you to resolve your promises concurrently rather than awaiting each result in the for loop. Depends what your use case is, really.
Re:Ask HN: What are some cool but obscure data structures yo...
by idontwantthis on Friday July 22, 2022 @09:04PM
(#32199134)
Use my approach or yours, but never use forEach with await. I don’t remember exactly why, but it just doesn’t work and will lead to very confusing errors.
Re:Ask HN: What are some cool but obscure data structures yo...
by meowtimemania on Friday July 22, 2022 @09:18PM
(#32199232)
Because Array.forEach doesn’t return a value or anything that could be awaited. If you use Array.map you can create an Array of promises that can be awaited with Promise.all
How bad is this? Should I switch to Promise.all instead?