Settings

Theme

Strict error handling in JavaScript with a functional try

github.com

1 points by ckmar 2 years ago · 1 comment

Reader

ckmarOP 2 years ago

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

Keyboard Shortcuts

j
Next item
k
Previous item
o / Enter
Open selected item
?
Show this help
Esc
Close modal / clear selection