[Proposal] Allow rest parameter on try... catch

Logan Smyth loganfsmyth at gmail.com
Wed Jul 15 17:35:05 UTC 2020


I'm not sure this fits nicely into things. The issue is that `Promise.all`
rejects as soon as it gets _any_ errors, so your `catch` block will run
before there are multiple errors. The syntax used around `Promise.all`
should not influence the behavior of `Promise.all` itself, so the only way
to do what you want would be to manually use `Promise.allSettled` or
something along those lines, and that would already provide the ability to
throw a singular array of errors, which would avoid the need for
`...errors` in the first place.

On Tue, Jul 14, 2020 at 11:29 PM Michaël Rouges <michael.rouges at gmail.com>
wrote:

> Hi all,
>
> My proposal's goal is really simple: provide a way to handle any numbers
> of errors, at once, in a synced try... catch.
>
> The problem:
> ------------------
> `
> async function reject (key) {
>   throw new Error(`error with key: ${key}`)
> }
>
> async function test () {
>   try {
>     await Promise.all([...new Array(3).keys()].map(reject))
>   } catch (error) {
>     console.error(error) // Error: error with key: 0
>   }
> }
>
> test()
> `
>
> Actually, we don't have a simple way to retrieve all errors in the catch
> and it can be difficult to manage when we don't know how many errors we can
> receive.
>
> Then, IMHO, allowing the rest parameter on the `catch (...errors)`
> resolves that problem, without breaking changes.
>
> What do you think about it, please?
>
>
> Cordially,
> Michaël Rouges - https://github.com/Lcfvs - @Lcfvs
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20200715/71ec810c/attachment.html>


More information about the es-discuss mailing list