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

278
Visualizações
nodejs - async.each function with async operation on each iteration

I have a question about async.each behavior. consider the code:

const examples = [1,2];

const asyncTask = (index) => {
    return new Promise(resolve => {
        setTimeout(() => {
            console.log(`Inside setTimeout-${index}`);
            resolve(true);
        }, 1500)
    });
}

function testingAsyncEach(callback){
    async.each(examples, (example, innerCallback) => {
        asyncTask(example).then(isDone => {
            console.log(`isDone: ${isDone}`);
            innerCallback(null);
        });
    }, err => {
        console.log('done testingAsyncEach');
        return callback()
    })
}

testingAsyncEach(() => {console.log('final callback testingAsyncEach');})

a simple code using the "async" module in nodejs, using the array [1,2] and on each item in the array executing the function "asyncTask" which returns a new promise which gets resolve after a timeout of 1.5 seconds.

in this scenario the output of the program is:

Inside setTimeout-1
isDone: true
Inside setTimeout-2
isDone: true
done testingAsyncEach
final callback testingAsyncEach

but when I changed the "testingAsyncEach" function to use the "await" syntax:

function testingAsyncEach(callback){
    async.each(examples, async (example, innerCallback) => {
        const isDone = await asyncTask(example);
        console.log(`isDone: ${isDone}`);
        innerCallback(null);
    }, err => {
        console.log('done testingAsyncEach');
        return callback()
    })
}

the async.each is not waiting for the "asyncTask" to end. output:

Inside setTimeout-1
isDone: true
done testingAsyncEach
final callback testingAsyncEach 
Inside setTimeout-2
isDone: true

Can you please help me understand why using the "await" instead of the "then" change the behavior? how can I still use the "await" syntax and get the proper output? Thanks!

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

According to the documentation of the async module, if you pass an async function, it won't pass the innerCallback to your function. I believe that your innerCallback(null) call therefore errored, making the async.each return early. (the 2nd example is already "launched" though, so that still happens afterwards)

Check if err is set to an error, and if so, log it.

Should that be the issue, removing the innerCallback argument and call should solve it.

over 4 years ago · Santiago Trujillo 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