Skip to content

Instantly share code, notes, and snippets.

@nvjkmr
Last active February 5, 2017 15:48
Show Gist options
  • Save nvjkmr/3607cc767f90e7dc5b0fc169994c7962 to your computer and use it in GitHub Desktop.
Save nvjkmr/3607cc767f90e7dc5b0fc169994c7962 to your computer and use it in GitHub Desktop.
// 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