2017-06-22 53 views
0

我认为a.shouldNotResolve()会在cancelOrder中“捕获”被拒绝的承诺,并返回“预计这会被捕获”,但它会解决,并返回“承诺无论如何解决”。为什么'shouldNotResolve`方法没有捕获被拒绝的承诺?

const a = { 

    cancelOrder: function(){ 
    return Promise.reject('something broke') 
    .then((x) => { 
     return x; 
    }) 
    .catch((e) => { 
     console.log('this caught the error', e); 
    }); 
    }, 

    shouldNotResolve: function() { 
    return this.cancelOrder() 
    .then(() => { 
     console.log('promise resolved anyway'); 
    }) 
    .catch(() => { 
     console.log('expected this to catch'); 
    }); 
    } 
} 

a.shouldNotResolve(); // "promise resolved anyway" 

为什么a.cancelOrder拒绝,但是a.shouldNotResolve无论如何解决?

谢谢。

回答

0

因为它已经赶上( “逮住”?)早些时候在柴n,如在cancelOrder函数中所见。如果你想在那里抓住它,但仍通过甩下来链则需要再次把它:

cancelOrder: function(){ 
    return Promise.reject('something broke') 
    .then((x) => { 
     return x; 
    }) 
    .catch((e) => { 
     console.log('this caught the error', e); 
     throw e; 
    }); 
    } 
1

因为你发现里面

cancelOrder: function(){ 
    return Promise.reject('something broke') 
    .then((x) => { 
     return x; 
    }) 
    .catch((e) => { // <-- this catches all errors 
     console.log('this caught the error', e); 
    }); 
    }, 

catch这个错误简直是像try-catch的承诺。如果你已经发现了一个外部的异常不会被捕获。

try { 
 
    try { 
 
    throw new Error('Failed') 
 
    } catch(e) { // first catch 
 
    console.log('inner', e.message) 
 
    } 
 

 
} catch(e) { // second catch 
 
    console.log('outer', e.message) 
 
}

由于@robertklep意味着你可能要重新掷

cancelOrder: function(){ 
    return Promise.reject('something broke') 
    .then((x) => { 
     return x; 
    }) 
    .catch((e) => { // <-- this catches all errors 
     console.log('this caught the error', e); 
     return Promise.reject(e) // re-reject 
    }); 
    }, 

const rejected1 = Promise.reject(1) 
 

 
const resolved = rejected1 
 
    .catch(x => console.log(x)) 
 
    .then(() => console.log('resolved')) 
 

 
const rejected2 = rejected1.catch(() => { 
 
    console.log('caught but rethrow') 
 
    return Promise.reject(2) 
 
}) 
 

 
rejected2.catch(x => console.log(x))

+1

如果错误被重新抛入'catch'处理程序中,它将正常工作。 – robertklep

0

写渔获cancelOrder承诺外流动,所以它不会把承诺回成功州。

const a = { 

    cancelOrder: function(){ 
    const result = Promise.reject('something broke') 
     .then((x) => { 
     return x; 
     }); 

    result.catch((e) => { 
     console.log('this caught the error', e); 
    }); 

    return result; 
    }, 

    shouldNotResolve: function() { 
    return this.cancelOrder() 
    .then(() => { 
     console.log('promise resolved anyway'); 
    }) 
    .catch(() => { 
     console.log('expected this to catch'); 
    }); 
    } 
} 

作为另一个答复中提到,另一种选择,如果你想要把catch在承诺的流动,是重新抛出的错误。

您也可能会考虑写这为您完成此拦截器:

function doCatch(fn) { 
    return function(reason) { 
    fn(reason); 
    throw reason; 
    }); 
} 

现在你可以写

cancelOrder: function(){ 
    return Promise.reject('something broke') 
     .then((x) => { 
     return x; 
     }) 
     .catch(doCatch(err => console.err("this caught the error", e))); 
    } 

顺便说一句,.then((x) => { return x; })是一个空操作。