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

163
Vistas
Get the final value in async loop

If anyone found this question is a dup, please comment a link to that, or any suggestions would be appreciated.

  • The request is to fetch data bases on an array of id.

  • Function getWorkhoursById() has already been configured with axios, including the Bearer and clientId, so I want to reuse it for this purpose.

  • So, now the result will be filled up with array of ids for each fetch, but it takes some time to reach the final value. My question is how can I retrieve the final value of arrayOfIds (with all the ids in the last iteration) to perform the next task.

  • I tried the for loop method - check the last value of it, but I would like to do this in a more proper, asynchronous way.

const arrayOfIds = [];
let result = [];

const foo = (item, index, id) => {
    return new Promise((resolve, reject) => {
      setTimeout(async () => {
        const data = await getWorkhoursById(id);
        if (data.length === 0) result.push(item);
        resolve();
      }, 1000 * (index + 1)); // force async function to execute in order
    });
  };
  const bar = () => {
      return arrayOfIds.forEach(async (item, index) => {
        names(item, index, id).then(() => {
          console.log(result, "result"); // <- need the last value of result
      });
    });
  };

about 4 years ago · Juan Pablo Isaza
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