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

90
Vistas
wait for function execution to end in array.map before next iteration
const handleEnter = (e) => {
        if (e.key === 'Enter') {
            const bulkSearchString = e.target.value;
            const jobIdArray = bulkSearchString.split(',').map((value) => value.trim());
            jobIdArray.map((jobId, i) => fetchEachJob(jobId, atsFilter.type)
                .then(result => {
                    if (result.length) {
                        const value = result[0];
                        const index = findIndex(formValuesId, (o) => o === value.id);
                        if (index < 0) {
                            setTimeout(
                                () => {
                                    addNewOption(
                                        value,
                                        atsFilter.filterType,
                                        atsFilter.type,
                                        atsFilter.radio,
                                        true
                                    );
                                },
                                500 * i
                            );
                        }
                    }
                }));
            e.preventDefault();
        }
    };

fetchEachJob: API call

addNewOption: Synchronous function

In the above code, I have added setTimeout so that addNewOption execution completes before it is called with new parameters. Without the setTimeout, the addNewOption is getting cancelled by the next call, thus returning result for only the last call. The code works fine with setTimeout but it is not the optimal solution.

Is there a way to wait for this synchronous function to finish before it is called by the next iteration?

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