该程序首先输出 0
然后189
。我知道这是由于 javascript 异步执行的。使两个日志输出 189
的正确方法是什么? ?不陷入回调 hell ?
roaming = [];
connection.query("SELECT land FROM roaming", function (err, res) {
if (err) {
console.log("Error: " + err);
} else {
res.forEach(function(entry){
roaming.push(entry.land);
});
console.log(roaming.length);
}
});
console.log(roaming.length);
最佳答案
我会使用异步/等待
async function asyncStuff() {
//async stuff
}
await asyncStuff();
关于javascript - 如何让我的 console.logs 输出相同的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43911207/