Created
November 1, 2016 12:20
-
-
Save gunar/cc4b65d752287d92111303e9bc6472c6 to your computer and use it in GitHub Desktop.
How to differentiate exceptions from rejections
This file contains hidden or 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
// 1. Using rejections for both exceptions and rejections | |
foo(5) | |
.then(bar) | |
.catch(err => { | |
if (!(err instanceof MyError)) {{ | |
// exception! | |
throw err | |
} | |
// else, process business logic error properly | |
}) | |
// 2. Using data.Task | |
// Exceptions will throw and task will never reject nor resolve | |
foo(5) | |
.fork(err => { | |
// business logic error | |
}, bar) | |
// 3. Returning errors inside a resolve | |
foo(5) | |
.then(bar => { | |
if (bar instanceof MyError) { | |
// handle business logic error | |
return | |
} | |
// ... | |
}) | |
.catch(err => { | |
// handle exception | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Gleb Bahmutov Mod Gunar C. Gessner • 2 hours ago