Filtered Promise#catch

Christopher Thorn morphcham at gmail.com
Wed Oct 11 00:28:00 UTC 2017


I agree that catch guards are useful, but they can be implemented in
userland fairly ergonomically:

```
function guard(predicate, callback) {
  return function guarded(reason) {
    if (!predicate(reason)) {
      throw reason;
    }
    return callback(reason);
  };
}

function instanceOf(constructor, callback) {
  return guard(reason => reason instanceof constructor, callback);
}

Promise.resolve('invalid')
  .then(JSON.parse)
  .catch(instanceOf(SyntaxError, reason => {
    // do something to handle the syntax error, or perhaps just silence it
  }));
```

Regards, Chris


More information about the es-discuss mailing list