Reputation: 28064
Assume the following code:
myModule.doStuff(options, function(err, results) {
if (err) console.log('e', err);
if (err) throw err;
console.log(results);
});
The offending code is:
return new Promise(function(resolve, reject) {
try {
throw new Error('test error');
resolve('success');
} catch (ex) {
console.log('r', ex);
reject(ex);
}
});
And then farther up the chain:
self.adapter
.collect(self.options)
.then(function(data) {
self.callback(null, data);
if (self.runnable) {
setTimeout(self.collect.bind(self), 2000);
}
}, function(error) {
self.callback(error);
});
The 'e [error info here]' statement prints, but the throw statement is apparently ignored. The app happily continues doing whatever else it was instructed to do and does not crash as expected. Why?
Upvotes: 2
Views: 571
Reputation: 28064
I never found the answer with the specific library I was using, but after switching to the Q library, it exposed a crucial step that I may have been missing before also. If there is no reject handler at the top level of the promise chain, the exception may be swallowed. According to the Q documentation, the way around this is to always call .done()
at the top of the chain which flushes any pending exceptions back to the caller.
Upvotes: 2