Agile Full Stack Software Engineer with extended experience in Business Intelligence. I am a passionate coder, expressing my creativity through elegant code.
iteration on arrays on a forEach loop does not result in synchronous running of promises, rather a for-loop with async/await would result in the problem you explained.
One idea I had some time ago was to use this method in order to obtain something similar to what the allSettled method will do:
Ah man, great points, all three! I went and edited to address (1) and (2). Depending on the example you're talking about with (3) above, that was more or less my point - running a bunch of awaited calls in a loop is sequential for better or worse.
Agile Full Stack Software Engineer with extended experience in Business Intelligence. I am a passionate coder, expressing my creativity through elegant code.
Sorry about the previous comment but I was replying from my mobile phone.
My point is that I would rewrite your third example like this:
constpostIds=['1','2','3','4','5'];asyncfunctionloadCommentsSequentially(ids){for(letidofpostIds){// wait for comments to be loaded and then load the comments from the next postreturnawaitloadComments(id);}}loadPostsSequentially().then((comments)=>...)
so that your next paragraph would make more sense:
There's a catch here, though - the await keyword will stop execution of the loop until loadComments returns for each post.
because in your example, using the forEach loop, the execution is not sequential, but rather a list of promises are created and are "lost" (nobody waits for them to be completed).
There are a couple of issues with your example:
One idea I had some time ago was to use this method in order to obtain something similar to what the allSettled method will do:
Promise.all([
promise1.catch(e => e),
promise2.catch(e => e)
]).then([ resultOrError, resultOrError ], ()=> ...)
Ah man, great points, all three! I went and edited to address (1) and (2). Depending on the example you're talking about with (3) above, that was more or less my point - running a bunch of
await
ed calls in a loop is sequential for better or worse.Thanks for reading, I appreciate your insight!
Hello again,
nice article, really :)
Sorry about the previous comment but I was replying from my mobile phone.
My point is that I would rewrite your third example like this:
so that your next paragraph would make more sense:
because in your example, using the forEach loop, the execution is not sequential, but rather a list of promises are created and are "lost" (nobody waits for them to be completed).
I hope I explained it better this time :)
Ahhhh yeah, I see I see - yep, that makes sense! Thanks again Davide!