等待蓝鸟执行所有承诺

时间:2017-10-23 13:34:14

标签: node.js promise bluebird

我想等待所有承诺解决或拒绝,并在.then中获得已解决的承诺的结果以及“

中的错误
'use strict';

const Promise = require('bluebird')

var delayOne = Promise.method((p = 1) => p);

var delayTwo = (p = 1) => {
    throw new Error('asf');
};

Promise.all([delayOne(100), Promise.method(delayTwo)(10), delayOne(300)])
    .then((res) => console.log(res)) // no logs from here
    .catch((e) => console.log(e))
    .done();

但我只从.catch获取日志。我的错误在哪里?

1 个答案:

答案 0 :(得分:2)

Promise.all等待履行的所有承诺,您希望得到拒绝的承诺,您可以在承诺失败的承诺上添加.reflect

Promise.all([
  delayOne(100), 
  Promise.method(delayTwo)(10).reflect(), // reflect here
  delayOne(300)])
  .then((res) => console.log(res)) // no logs from here
  // don't `.catch(e => console.log(e))` it is not needed
  // done is deprecated, you don't need it