Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

280
Visualizações
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);
});
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

  • 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);
});
about 4 years ago · Juan Pablo Isaza Relatório

0

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);
});
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda