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

291
Views
Async Function: Which Console.log Is called first?

My question is in reference to the code below. When I call this file on my terminal "node app.js"...I'm expecting to see [B] first before [A] since the function addAddress(res) is Async and it has the keyword await so it should be completed first before moving on/is called before the console.log("[A]"). Also, my understanding was that the Last item in a CallStack is also the first item to be called/executed Out (LASTinFirtsOut). So since the function addAddress(res) was last called it should be executed before continuing.

Thank you all once again looking forward to furthering my understanding.

const mongoose = require('mongoose');

mongoose.connect('mongodb://localhost:27017/relationshipDemo', { useNewUrlParser: true, useUnifiedTopology: true })
    .then(() => {
        console.log("MONGO CONNECTION OPEN!!!")
    })
    .catch(err => {
        console.log("OH NO MONGO CONNECTION ERROR!!!!")
        console.log(err)
    })


const userSchema = new mongoose.Schema({
    first: String,
    last: String,
    addresses: [
        {
            _id: { id: false },
            street: String,
            city: String,
            state: String,
            country: String
        }
    ]
})


const User = mongoose.model('User', userSchema);

const makeUser = async () => {
    const u = new User({
        first: 'Harry',
        last: 'Potter',
    })
    u.addresses.push({
        street: '123 Sesame St.',
        city: "New York",
        state: 'NY',
        country: 'USA'
    })
    const res = await u.save()
    addAddress(res)
    console.log(res);
    console.log("[A]")
}



const addAddress = async (id) => {
    const userNow = await User.findById(id);
    userNow.addresses.push({
        street: '123 Florida',
        city: "Miami",
        state: 'FL',
        country: 'USA'
    })
    const userSaved = await userNow.save();
    console.log(userSaved)
    console.log('[B]')
}

makeUser()
about 4 years ago · Juan Pablo Isaza
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!