简体   繁体   中英

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

I find that I cannot get the response when using promise.all .

The orders below should be an array

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
I update the code based on the comment.
The orders is now returning

(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 ?

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
  • You aren't calling resolve anywhere
  • You don't need to construct a Promise when you're calling a function that already gives you a 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.

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.

If you want to include only items for which functionC is called, filter out the empty values afterwards.

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. Avoid the Promise constructor antipattern and never pass an async function as the executor to new Promise !

This code can be greatly simplified by consequent usage of async / await instead of .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);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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