Skip to content

Instantly share code, notes, and snippets.

@humanchimp
Created October 10, 2017 23:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save humanchimp/0fe143f5615f907ad7954e42ae5cfd38 to your computer and use it in GitHub Desktop.
Save humanchimp/0fe143f5615f907ad7954e42ae5cfd38 to your computer and use it in GitHub Desktop.
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
}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment