0

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);
});
CCCC
  • 5,665
  • 4
  • 41
  • 88
  • 2
    Avoid the [`Promise` constructor antipattern](https://stackoverflow.com/q/23803743/1048572?What-is-the-promise-construction-antipattern-and-how-to-avoid-it) and [never pass an `async function` as the executor to `new Promise`](https://stackoverflow.com/q/43036229/1048572)! – Bergi Jan 28 '22 at 03:54
  • why are you making a prose (not using its resolve) when you have a promise from the functionA? – epascarello Jan 28 '22 at 03:58

2 Answers2

2
  • 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);
});
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
1

Well, you never resolve() or reject() the new Promises 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);
});
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • please check my update above – CCCC Jan 28 '22 at 04:09
  • @CCCC You're getting the `undefined` values from the `else` cases where the asynchronous function doesn't return anything. Just filter them out before logging the results. – Bergi Jan 28 '22 at 12:14