7

This is the way.

Comments
  • 1
    I literally needed this right now, thanks!
  • 0
    @homo-lorens

    Me to Promise.allSettled():

    "Just go and do all this shit and let me know what happened... whenever that is."
  • 3
    Unless you can absolutely ignore errors and returned values, it's still a better idea to do something like

    const [result1, result2, result3, ...] = await Promise.all([
    p1.catch(err => ...),
    p2.catch(err => ...),
    p3.catch(err => ...),
    ...
    ])

    With allSettled you can't assign returned values without referencing them by index, and you lose context (e.g. when you map an array to promises), which means you again need to reference items by index to log some meaningful info on errors. Also you're splitting item-level logic across multiple iterations, which makes your code much harder to understand.
  • 1
    @hitko

    I see what you mean, that's a good point when you want to be a bit more granular about who, what, where and etc came back.

    In this case I'm doing a bunch of DB updates, all I want to know is who was successful and who wasn't. The successful responses I know what they'll look like so that's obvious by what is returned, and they tell me their id anyhow so I know who they are regardless of order.

    If it fails I just note it as an error, log it, and notify the user that it didn't work and I have a process from there for them to retry and so on.

    But good point. I like that method / might use it if I want to orchestrate a bit more.
  • 1
    @N00bPancakes how about?

    Promise.broken()
  • 0
    @Devnergy

    Dose that just return null randomly?
Add Comment