Last active
February 5, 2017 15:48
-
-
Save nvjkmr/3607cc767f90e7dc5b0fc169994c7962 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
// Initialization | |
var log = ""; | |
function doWork() { | |
log += "W"; | |
return Promise.resolve(); | |
} | |
function doError() { | |
log += "E"; | |
throw new Error("oops!"); | |
} | |
function errorHandler(error) { | |
log += "H"; | |
} | |
// snippet 1 | |
doWork() | |
.then(doWork) | |
.then(doError) | |
.then(doWork) // this will be skipped | |
.then(doWork) // this will be skipped | |
.catch(errorHandler) | |
// 'log' variable contains: "WWEH" | |
// snippet 2 | |
doWork() | |
.then(doWork) | |
.then(doError) | |
.then(doWork) // this will be skipped | |
.then(doWork, errorHandler) // doWork will be skipped | |
// 'log' variable contains: "WWEH" | |
// the basic difference between the above two snippets show up when we do the following: | |
// rewriting snippet 1 | |
doWork() | |
.then(doWork) | |
.then(doWork) | |
.then(doWork) | |
.then(doError) | |
.catch(errorHandler) | |
// 'log' variable contains: "WWWWEH" | |
// rewriting snippet 2 | |
doWork() | |
.then(doWork) | |
.then(doWork) | |
.then(doWork) | |
.then(doError, errorHandler) // errorHandler is skipped | |
.catch(errorHandler) // will be caught here (instead of above) | |
// 'log' variable contains: "WWWWEH" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment