mags mags - 1 year ago 86
Javascript Question

"throw new Error()" caught in catch block context in mocha.js

******Edited from original

I'm throwing an error from the "catch" block of a promise. How can I get the error to get passed on from that catch block to my mocha test.

it('should fail as pending', function(done){
var p = new Promise(function(resolve, reject) {
setTimeout(function() {
}, 1000);


p.then(function() {
//resolve outer promise
}).catch(function() {
//throw error should reject outer promise
done(new Pending('I want to get out of this catch block!'));


Answer Source

The catch handlers in a Promise are just as asynchronous as the promise itself, so you can't revert back to synchronous flow/exception control from them. The function you're passing into .catch may not be called until well after the try/catch surrounding it has exited.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download