1 comment

[ 5.0 ms ] story [ 11.6 ms ] thread
One line of code which is specific in what it catches:

    // Either foo or someError will be defined
    const [foo, someError] = itry(someFn, SomeError);
Instead of nine lines which feel like they are working uphill against the language: dealing with variable scoping issues, deeper nesting, and footguns like forgetting to re-throw.

    let foo;
    try {
      foo = someFn();
    } catch (err) {
      if (err instanceof SomeError) {
        // do something with error
      } else {
        throw err;
      }
    }
    // use foo