Promise forking and error handling
JavaScript
function handleUnexpectedError(reason) {
console.error(reason)
}
function handleUnexpectedErrorFactory(prefix) {
return function(reason) {
console.error('[' + prefix + '] ' + reason)
}
}
var get42 = function() {
return new Promise(function(resolve, reject) {
console.log('tt1');
if (false) {
//if (true) {
resolve(42);
} else {
//reject('not lucky today');
throw 'not lucky today';
}
});
};
function getNumber () {
console.log('tt0');
var promise = get42();
console.log('tt2');
// we do a fork here, because we return `promise`, not the chain of promise.then.then.then
// promise.then(function(value) {
// console.log('fork 1, the value is: ' + value);
// }).then(function(){
// console.log("B")
// }).then(function(){
// console.log("C")
// //});
// //}).catch(handleUnexpectedError);
// }).catch(handleUnexpectedErrorFactory('fork 1'));
return promise;
}
getNumber();
// getNumber().then(function(value) {
// console.log('fork 2, the value is: ' + value);
// //});
// //}).catch(handleUnexpectedError)
// }).catch(handleUnexpectedErrorFactory('fork 2'))