Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

295
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda