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

# Michaël Rouges (3 months ago)

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 - Lcfvs - @Lcfvs

# Logan Smyth (3 months ago)

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.

# Michaël Rouges (3 months ago)

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 - Lcfvs - @Lcfvs

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