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