in the for loop, how to get the data asynchronously through mongoose, and how to execute the following commands after all the asynchronous methods are finished.
pseudo code is as follows:
function getData (list) {
let result = []
list.forEach((item) => {
if (item.type === 1) {
//
result.push(x)
} elseif (item.type === 2) {
//
result.push(y)
} else {
//
result.push(z)
}
})
return result
}
function main () {
//
let result = getData (list)
}
after execution, the result data is all empty, mainly due to the fact that the asynchronous task has not yet been executed. How to solve this problem?