我有一系列正在解决的 PromisePromise.all(arrayOfPromises);
我继续继续承诺链。看起来像这样
existingPromiseChain = existingPromiseChain.then(function() {
var arrayOfPromises = state.routes.map(function(route){
return route.handler.promiseHandler();
});
return Promise.all(arrayOfPromises)
});
existingPromiseChain = existingPromiseChain.then(function(arrayResolved) {
// do stuff with my array of resolved promises, eventually ending with a res.send();
});
我想添加一个 catch 语句来处理单个承诺,以防出错,但是当我尝试时,Promise.all
返回它找到的第一个错误(忽略其余的),然后我无法从数组中其余的承诺中获取数据(没有错误)。
我尝试过做类似的事情..
existingPromiseChain = existingPromiseChain.then(function() {
var arrayOfPromises = state.routes.map(function(route){
return route.handler.promiseHandler()
.then(function(data) {
return data;
})
.catch(function(err) {
return err
});
});
return Promise.all(arrayOfPromises)
});
existingPromiseChain = existingPromiseChain.then(function(arrayResolved) {
// do stuff with my array of resolved promises, eventually ending with a res.send();
});
但这并不能解决问题。
--
Edit:
下面的答案完全正确,代码由于其他原因而被破坏。如果有人感兴趣,这就是我最终得到的解决方案......
Node Express服务器链
serverSidePromiseChain
.then(function(AppRouter) {
var arrayOfPromises = state.routes.map(function(route) {
return route.async();
});
Promise.all(arrayOfPromises)
.catch(function(err) {
// log that I have an error, return the entire array;
console.log('A promise failed to resolve', err);
return arrayOfPromises;
})
.then(function(arrayOfPromises) {
// full array of resolved promises;
})
};
API 调用(route.async 调用)
return async()
.then(function(result) {
// dispatch a success
return result;
})
.catch(function(err) {
// dispatch a failure and throw error
throw err;
});
把.catch
for Promise.all
之前.then
似乎已经达到了捕获原始承诺中的任何错误的目的,但然后将整个数组返回到下一个.then
.