Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

173
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda