简体   繁体   English

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

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

I find that I cannot get the response when using promise.all .我发现使用promise.all时无法得到响应。

The orders below should be an array下面的orders应该是一个数组

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

Full code完整代码

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
});

How can I fix?我该如何解决?

Update 1更新 1
I update the code based on the comment.我根据评论更新代码。
The orders is now returning 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]

How can I make the promise returns without undefined ?如何使 promise 返回没有undefined

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);
});
  • The Promise constructor ignores any value returned from its callback Promise 构造函数忽略从其回调返回的任何值
  • You aren't calling resolve anywhere你没有在任何地方打电话resolve
  • You don't need to construct a Promise when you're calling a function that already gives you a Promise当您调用已经为您提供 Promise 的 function 时,您不需要构建 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);
});

A better approach would be to utilize await to make the code flat and easy to read.更好的方法是利用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
    }
});

You also might consider handling errors in the Promise.all , not in the mapper.您还可以考虑在Promise.all中处理错误,而不是在映射器中。

If you want to include only items for which functionC is called, filter out the empty values afterwards.如果您只想包含调用functionC的项目,请在之后过滤掉空值。

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

Well, you never resolve() or reject() the new Promise s you're creating.好吧,您永远不会resolve()reject()您正在创建的new Promise Avoid the Promise constructor antipattern and never pass an async function as the executor to new Promise !避免使用Promise构造函数反模式,并且永远不要将async function作为执行者传递给new Promise

This code can be greatly simplified by consequent usage of async / await instead of .then() :通过随后使用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);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM