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

331
Visualizações
async/await not working in NodeJS, it doesn't wait

I'm calling 2 REST API endpoints using Axios in NodeJS.I'm trying to make that the code executes line by line but the second (viewProfile()) function doesn't wait for generateId();

const generateId = async (data) => {
    const options = optionsBuilder("post","profile", data);
    try {
        const response = await axios(options);
        id = response.data.id;
        console.log(id);
    } catch (error) {
        console.error(error);
    }
}
const viewProfile = async (profileId) => {
    console.log('Test 2', profileId);
    const path = `blog/${profileId}`;
    const options = optionsBuilder("get", path);
    try {
        const response = await axios(options);
        console.log(response.data);  
    } catch (error) {
        console.log(error);
        }
    }
}
...
generateId(data);
viewProfile(profileId);
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You must execute them into an async scope:

(async function(){
    await generateId(data);
    await viewProfile(profileId);
})()    

Otherwise they aren't parsed to be awaited and viewProfile will be executed right after generateId. :)

As, if you are using the new ES2022 you can just await into the top level.

For newer Node.js versions:

await generateId(data);
await viewProfile(profileId);
about 4 years ago · Juan Pablo Isaza 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