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

Michaël Rouges michael.rouges at gmail.com
Thu Jul 16 07:13:42 UTC 2020

 Yeah, `Promise.allSettled` is better than `Promise.all()` for that... but,
perhaps, it can be useful to improve it to provide a way to enforce the
fulfillment OR to catch only the errors if any, but the real idea is to
have a way to handle the errors, using a `try... catch(...errors)` or a
`promise.catch(...errors)`, without any tricks.

Something like this:

Promise.allSettled(promises, true) // true enforces the fulfillment
  .then(([...results]) :=> {}) // no errors
  .catch((...errors) => {}) // errors only

In addition, I don't think about an array of errors but really a rest
parameter to be able to get the first error, as actually, or all of them to
avoid any problems when the catch handler doesn't know if the fulfillment
is enforced or not.

Michaël Rouges - https://github.com/Lcfvs - @Lcfvs

Le mer. 15 juil. 2020 à 19:35, Logan Smyth <loganfsmyth at gmail.com> a écrit :

> 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/20200716/b6c95caf/attachment.html>

More information about the es-discuss mailing list