function resolveAfter2Seconds(x) {
return new Promise(resolve => {
setTimeout(() => {
resolve(x);
}, 2000);
});
}
async function f1() {
var x = await resolveAfter2Seconds(10);
console.log(x);
}
f1();
let x = 3;
为什么会看到以下情况?
输入f1。停止等待。
从f1返回(console.log(x)命令未执行)
为x分配3(错误!等待跳过,js向前执行)
返回“ console.log(x)”行上的f1。打印x。
为什么JS不等待等待并向前迈进?你能给我个建议吗?
呼如林
阿晨1998
相关分类