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

174
Visualizações
javascript - not able to execute the promise from array one by one

I found that I cannot resolve the promises one by one.

This is my code

const promises = mkt.map((marketItem) => {
  return context.program.account.chain
    .fetch(marketItem[0].instrument)
    .then((instrumentRes) => {
      return Test(context, marketItem[0]).then(
        testResult => {
          return Promise.all([
            functionA(),
            functionB(),
          ]).then((result) => {
            return result
          });
        }
      );
    });
});
console.log(promises)
for (let i=0; i < promises.length; i++) {
  const val = await promises[i]();
  console.log(val);
}

error

promises[i]() is not a function

Why is that?How can I solve it?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

promises[i]() is not a function

Correct, this is because promises is an array of Promise objects, not functions.

You can dump this promises array into Promise.all and wait for them all to resolve.

Promise.all(promises)
  .then(resolvedPromises => {
    // handle array of resolved promises
  });

but I am working on some real time update stuff. If I use Promise.all it will throw error Too many request, so I want to do it one by one to see the effect

For this then I think you want to iterate on the mkt array and wait for each created Promise to resolve. Refactor the mapping callback into a standalone function that you can manually invoke within the for-loop, awaiting the the Promise to settle.

const request = (marketItem) => {
  return context.program.account.chain
    .fetch(marketItem[0].instrument)
    .then((instrumentRes) => Test(context, marketItem[0]))
    .then(testResult => Promise.all([functionA(), functionB()]))
    .then((result) => result);
}

for (let i=0; i < mkt.length; i++) {
  try {
    const val = await request(mkt[i]);
    console.log(val);
  } catch(error) {
    // handle any error
  }
}
about 4 years ago · Juan Pablo Isaza Relatório

0

Just removing the () from the await call should already work.

  const val = await promises[i];

However keep in mind that all the promises have already been "started" (I don't know a better word for that) at that point and you're just retrieving the results or an error with the await call.

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