News for hackers. Stuff that matters.
Re:What's in the Box? by rattray on Wednesday April 21, 2021 @12:44AM (#26885501)
I really like this pattern in TS:

    const x: Data | Error = await fetchData().catch(e => e);
    if (x instanceof Error) {
      return 'too bad'
    }
    // proceed with x as Data
Basically you add `.catch(e => e)` to any I/O which may fail, and handle the error in an `if` instead of a `try`.

Many of the benefits of Go's error handling, but without the awkward extra variable and without a chance of using your result without ensuring it isn't an error.

There are no comments on this comment yet.