Skip to content

Instantly share code, notes, and snippets.

@bguiz
Last active July 1, 2016 05:37
Show Gist options
  • Save bguiz/c974941c838b0f4a01d4aef6b23165e1 to your computer and use it in GitHub Desktop.
Save bguiz/c974941c838b0f4a01d4aef6b23165e1 to your computer and use it in GitHub Desktop.
`.catch()` doesn't stop the rest of a promise chain from executing
// returns a promise that always rejects
function myPromise() {
return new Promise((resolve, reject) => {
reject('error in myPromise');
});
}
// promise.catch(a).then(b).catch(c)
myPromise().catch(function a(err) {
console.log('a:', err);
return 'return in a';
}).then(function b(result) {
console.log('b:', result);
throw 'error in b';
}).catch(function c(err) {
console.log('c:', err);
});
/* Output:
a: error in myPromise
b: return in a
c: error in b
*/
// promise.then(x1, x2).then(y).catch(z)
myPromise().then(function x1(result) {
console.log('x1:', result);
return 'return in x1';
}, function x2(err) {
console.log('x2:', err);
return 'return in x2';
}).then(function y(result) {
console.log('y:', result);
throw 'error in y';
}).catch(function z(err) {
console.log('z:', err);
});
/* Output:
x2: error in myPromise
y: return in x2
z: error in y
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment