JSFiddle - React, Tailwind, and code Playground
JavaScript
/*
* myCustomError()
* from http://stackoverflow.com/a/5251506/3478010
*/
function myCustomError(message) {
this.name = 'myCustomError';
this.message = message;
this.stack = (new Error()).stack;
}
myCustomError.prototype = new Error;
function fn(n) {
return new Promise(function(resolve, reject) {
setTimeout(function() {
if(n >= 0) {
console.log(n);
resolve(n || 0);
} else {
// reject(new Error(n || 0));
reject(new myCustomError(n || 0));
}
}, 500);
});
}
function fError(error) {
// if ((+error.message || 0) < 0) { // <<<<< must be a reliable test for a previous error
if (error instanceof myCustomError) {
throw error;
} else {
console.log(error);
}
}
function finalErrorHandler(error) {
console.log('Final: ' + error.message);
}
var Function1 = fn.bind(null, 1);
var Function2 = fn.bind(null, -2);
var Function3 = fn.bind(null, 3);
var Function4 = fn.bind(null, 4);
var errorHandler1 = fError;
var errorHandler2 = fError;
var errorHandler3 = fError;
var errorHandler4 = fError;
Promise.resolve()
.then(Function1).catch(errorHandler1)
.then(Function2).catch(errorHandler2)
.then(Function3).catch(errorHandler3)
.then(Function4).catch(errorHandler4)
.catch(finalErrorHandler);