DEV Community

Discussion on: Solve* all your problems with Promise.allSettled()

Collapse
 
irreverentmike profile image
Mike Bifulco

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.

Thanks for reading, I appreciate your insight!

Collapse
 
brokenseal profile image
Davide Callegari • Edited

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:

const postIds = ['1', '2', '3', '4', '5'];

async function loadCommentsSequentially(ids) {
  for (let id of postIds) {
    // wait for comments to be loaded and then load the comments from the next post
    return await loadComments(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).

I hope I explained it better this time :)

Thread Thread
 
irreverentmike profile image
Mike Bifulco

Ahhhh yeah, I see I see - yep, that makes sense! Thanks again Davide!