I have a series of promises where if any of them throw an error it bubbles up to the top promise - kind of like global error catching.
Here's a simplified version of how I've got things setup:
function bar() {
return new Promise(function(resolve,reject) {
var err = new Error('This is an error');
err.name = 'Permission';
return reject(null,err);
});
}
function foo() {
return new Promise(function(resolve,reject) {
bar()
.then(function(results) {
return resolve(results);
})
.then(null,function(error) {
console.log('Error caught'); //This shows
console.log(error); //outputs null
return reject(error);
});
});
}
foo()
.then(function(results) {
console.log(results);
})
.then(null,function(error) {
//this never gets reached
});
For some reason although the error function runs the value of 'error' is null. Any ideas on what would cause this?
Thanks!
reject(err)in yourbarfunction for not gettingnullfoofunction uses the promise constructor antipattern!function foo() { return bar().catch(function(err) { console.log("Error caught and rethrown", err); throw err; }) }