Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

335
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!