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

89
Visualizações
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 à 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