javascript - 无法从 promise.all() 获得响应

javascript - unable to get response from promise.all()

我发现在使用promise.all时无法得到响应。

下面的orders应该是数组

Promise.all(promises).then(function (orders) {
  console.log(orders); // nothing logged
});

完整代码

let promises = markets.map(async (mkt) => {
  return new Promise(async function (resolve, reject) {
    return functionA(paramA)
      .then(resA => {
        return res.functionB()
          .then((resB) => {
            if (resB.length > 0) {
              return functionC()
                .then(resC => {
                  console.log(resC); // successfully logged
                  return resC;
                });
            }
          });
      })
      .catch(err => console.log(err));
  });
});
Promise.all(promises).then(function (orders) {
  console.log(orders); // nothing logged
});

我该如何解决?

更新 1
我根据评论更新了代码。
orders 现在回来了

(54) [undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, Array(1), undefined, undefined, undefined, undefined, undefined, undefined, undefined, Array(1), undefined, Array(1), undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined]

没有undefined我怎么能做出returns的承诺呢?

const promises = markets.map(async mkt => {
  const resA = await functionA(paramA);
  const resB = await res.functionB();
  if (resB.length > 0) {
    const resC = await functionC()
    console.log(resC); // successfully logged
    return resC;
  }
});
Promise.all(promises).then(orders => {
  console.log(orders); // nothing logged
}, err => {
  console.log(err);
});
  • Promise 构造函数忽略从其回调返回的任何值
  • 您没有在任何地方打电话 resolve
  • 当你调用一个已经给你一个 Promise 的函数时,你不需要构造一个 Promise
const promises = markets.map((mkt) => {
    return functionA(paramA)
        .then(resA => res.functionB())
        .then((resB) => {
            if (resB.length > 0) {
                return functionC()
                    .then(resC => {
                        console.log(resC); // successfully logged
                        return resC;
                    });
            }
        })
        .catch(handleError);
});

更好的方法是利用 await 使代码扁平化且易于阅读。

const promises = markets.map(async (mkt) => {
    try {
        const resA = await functionA(paramA);
        const resB = await res.functionB();

        if (resB.length > 0) {
            const resC = await functionC()
            return resC;
        }
    } catch (e) {
        // handle errors
    }
});

您也可以考虑在 Promise.all 中处理错误,而不是在映射器中。

如果您只想包含调用了 functionC 的项目,请在之后过滤掉空值。

Promise.all(promises).then(function (orders) {
  const filtered = orders.filter(Boolean);
  console.log(filtered);
});

好吧,您永远不会 resolve()reject() 您正在创建的 new Promise。避免 Promise constructor antipattern and !

此代码可以通过随后使用 async/await 而不是 .then():

来大大简化
const promises = markets.map(async mkt => {
  const resA = await functionA(paramA);
  const resB = await res.functionB();
  if (resB.length > 0) {
    const resC = await functionC()
    console.log(resC); // successfully logged
    return resC;
  }
});
Promise.all(promises).then(orders => {
  console.log(orders); // nothing logged
}, err => {
  console.log(err);
});