If you are into web development, there is a 100% chance you have used at least some async functions. There are different ways to use async function...
For further actions, you may consider blocking this person and/or reporting abuse
With the existence of
Promise.all
, this is definitely the "right" way of doing this, but it isn't technically the only one:As you can probably tell, this approach can lead to much less readable code, which is why a solution like
Promise.all
makes sense.But we can also implement our own
Promise.all
this way:To make the code shorter, this version just writes the results back into the same array and ignores rejected promises, but it shows how there isn't actually any magic behind
Promise.all
.Your
simple_promise_all
isn't equal to Promise.all though, no?Your's doesn't reject if any reject, and it waits for each resolve/reject sequentially, where as Promise.all tries to resolve all of them at the same time.
In your example, with 10 requests that each take approx. 2 seconds to resolve, you would need to wait 20 seconds. With Promise.all it would be 2 seconds.
Yes, as I pointed out, I ignored that feature to keep my example short.
Both approaches resolve all of the promises at the same time, one of them just hides the implementation so you don't really think about how it's done.
It would be 2 seconds (give or take) in both cases.
await
ing a promise doesn't pause the others, so while the loop is waiting for the first one, the other promises are all still running. Once the first one finishes, the other promises will all be finished too or just about to, soawait
ing them is basically just collecting their results.You are right, I brain farted 😂
Just for fun, here's a more complete implementation:
If you remove the "p instanceof Promise" check it still works fine, but it also handles the case where "p" is a then-able; that is a promise that doesn't extend Promise. Await can be used on synchronous values without trouble, so no need to branch.
Good point!
A very important note for using timeouts with Promise.race()
When you have a timeout, while the promise may resolve or reject, the timeout will keep running, keeping the execution alive.
So if the success case passes after 1 second and the program's logic ends, the timeout set for 5 seconds will keep going for another 4 seconds and only then will the process' execution end.
This can be really important in serverless Lambdas where you pay for run time.
In the above example, if the fetch API call succeeds after 500ms, the process ends.
Without clearing the timer as in your example, the process would continue for another 9.5 seconds before ending.
(On mobile, forgive any typos/autocorrects)
Definitely something to keep it mind!
A better way could be using effect.website/
Will definitely check it out, Thanks!
I definitely learned something new, Aditya! Thanks for this amazing blog post! Had to bookmark this one! Thanks again! 💯
I'm really glad, you got to learn!
Thanks for sharing Aditya. Helped me a lot. Nice brief examples as well.
I have learned a lot! Thanks, Aditya!
Very interesting Aditya!
Recently learnt promises and was looking for practical use cases of these methods. This is the best explanation I found. Thanks a lot!!! 💛🖤
In my opinion async func is grammar in ES-6. So I think it's better method in javascript.