Question. Have you checked the performance using Promise.all? While this might not be the use-case. I was thinking that maybe there is a difference in speed of execution if you're going to use Promise.all.
I think there would be performance gain using Promise.all if you were comparing it to Oğuzhan’s first example.
Assuming pokemon API takes 3 seconds to respond and digimon API takes 10 seconds to respond (and that they both always succeed). Oğuzhan’s example would take 13 seconds (the sum of each response) before getting to any pokemon related logic, whereas Promise.all would bring that down to 10 seconds (the length of the longest response).
However, if you sent them off in parallel as per my example then you can get to the pokemon related logic as soon as thats returned, 3 seconds! And while you’re doing the pokemon logic, the digimon response is still coming back in time for when you need to do digimon logic.
This. But instead of using Promise.all, use Promise.allSettled. Promise.all will reject everything if any one of the Promises fail, whereas allSettled will return each Promises state so you can handle the resolved and rejected ones appropriately.
Hmmm, no, I really don't think so. As Simon said above, if you call all Promises at the same time then it will only take as long as the longest one to resolve, rather than waiting for the first, then moving on to the second, etc.
Question. Have you checked the performance using
Promise.all
? While this might not be the use-case. I was thinking that maybe there is a difference in speed of execution if you're going to use Promise.all.I think there would be performance gain using
Promise.all
if you were comparing it to Oğuzhan’s first example.Assuming pokemon API takes 3 seconds to respond and digimon API takes 10 seconds to respond (and that they both always succeed). Oğuzhan’s example would take 13 seconds (the sum of each response) before getting to any pokemon related logic, whereas
Promise.all
would bring that down to 10 seconds (the length of the longest response).However, if you sent them off in parallel as per my example then you can get to the pokemon related logic as soon as thats returned, 3 seconds! And while you’re doing the pokemon logic, the digimon response is still coming back in time for when you need to do digimon logic.
This. But instead of using
Promise.all
, use Promise.allSettled.Promise.all
will reject everything if any one of the Promises fail, whereasallSettled
will return each Promises state so you can handle the resolved and rejected ones appropriately.In some cases
Promise.All
out performsAsync/Await
. In most they are roughly the same.Hmmm, no, I really don't think so. As Simon said above, if you call all Promises at the same time then it will only take as long as the longest one to resolve, rather than waiting for the first, then moving on to the second, etc.
James, you were right. Since
Promise.All
was not the topic of this article but I ran some code to test it here the results.Promise.All with loops:
Promise.All without loops: