2017-04-23 121 views
0

大家好,感谢提前寻求帮助。蓝鸟promise.all在中期承诺链捕捉并继续承诺

下面是什么我试图做

function1(){ 
    throw some error(); 
} 
function2() { 
    // dosomething successfully; 
} 

promise.resolve() 
    .then() 
    .then(
     // here i want to do promise.all and if there is any exception i want to continue with chain 
     promise.all(function1, function2) 
     .catch() // handle error here only 
) 
    .then() 
    .then() 
    .catch() 

谁能帮我,我怎么能做到这一点。即在做promise.all时,如果有任何错误。我不想打破这个承诺链。

+0

在问这个模式应该返回(预期的结果,其中语法修正,包括''内'前。这时Promise.all()'调用return')' – guest271314

回答

2

尝试:

Promise.resolve().then(()=> { 
    return Promise.all(promisesArray).catch((err) => console.log(err)) 
}).then(() => console.log('continue futher')); 
0

如果您对Promise.all()一个.catch(),你不从抓投入或不返回拒绝承诺,那么承诺链将继续与任何值.catch()回报。

它的建模完全在try/catch之后,除非你重新抛出,否则捕获将停止异常。同样,.catch()可以阻止拒绝,除非您从捕获处理程序重新抛出(或返回拒绝的承诺)。

另外,你的代码流是不太正确的。你有Promise.all()这里的方式是不正确的:

Promise.resolve().then(...).then(Promise.all(...).catch(...)).then(...) 

你必须给一个函数传递给.then()没有要求Promise.all()这是一个承诺的结果。当调用Promise.all()时,您需要传递一组promise。

你可以这样说:

Promise.resolve() 
    .then(...) 
    .then(function() { 
     return Promise.all([funcReturnsPromise1(), funcReturnsPromise2()]).catch(...); 
    }).then(...)