我有一系列要解决的 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,以防它出错,但是当我尝试时,Promise.all
返回它发现的第一个错误(忽略其余的),然后我无法从promise的其余部分获取数据数组(没有错误)。
我试过做类似..
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();
});
但这并不能解决。
谢谢!
--
编辑:
下面的答案完全正确,由于其他原因,代码被破坏了。如果有人感兴趣,这是我最终得到的解决方案......
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放在 thePromise.all
之前.then
似乎已经达到了从原始承诺中捕获任何错误的目的,然后将整个数组返回到下一个.then
谢谢!