异步函数:哪个Console.log首先称为?

发布于 2025-02-10 13:02:19 字数 1718 浏览 1 评论 0 原文

我的问题是参考以下代码。当我在终端“ node app.js”上调用此文件时...我希望在 [a] 之前先看到 [b] ,因为功能 adddress(res)是异步,它具有关键字等待,因此在 console.log.log(“ [a]”之前,请先在继续/打电话之前首先完成/调用。 )。另外,我的理解是,呼叫站中的最后一项也是第一个被拨打/执行的项目(LastFirtSout)。因此,由于函数辅助(res)在继续之前应执行。

再次感谢大家,期待着进一步的理解。

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()

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()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文