Try-catch oneliner

what would try { a: someReallyLongComplexFunctionThatRequiresLotsOfParsing } produce? is that an object with an "a" key in a try expression, or an "a" label, inside a try statement block?

This is a very good point, actually. OK, there is a couple ways of solving such situation:

Operator try* or *try can be introduced (similar to generators):

const [error, data] = try* JSON.parse('xx');

Maybe using round bracket to disambiguate the expression form (like we do for assignment object destructuring)?

try can be used as function similar to dynamic import

const [error, data] = try(JSON.parse, 'xxx');

functional approach, not as obvious as previous variant.

A new keyword aim (or attempt) can be introduced:

const [error, data] = aim JSON.parse('xxx');

But it can break some existing code.
I think first example is the most straight forward, for await it can look like:

const [error, data] = try* await fetch(url);

Form can be discussed, but using try as expression can be very useful, and simplify code a lot in a similar to throw expressions way.

1 Like