var data
user.save(function (err, doc) {
if (err) {
console.log(err)
} else {
Log (doc)//successfully output the document object of the operation
data = doc //undefined
}
})
console.log(data) //undefined
Due to asynchrony, data cannot get the value of doc. Now it needs return data. How can I change the code?
I can think of two ways:
- Continue executing the code you want to execute in the callback
- Use
async / await
Wait for asynchronous execution to complete