Created
October 10, 2017 23:39
-
-
Save humanchimp/0fe143f5615f907ad7954e42ae5cfd38 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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