我有一系列要解决的 Promise Promise.all(arrayOfPromises);
我继续继续Promise链。看起来像这样
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;
    });
将.catchfor放在 thePromise.all之前.then似乎是为了从原始Promise中捕获任何错误,但随后将整个数组返回到下一个.then
谢谢!