为什么在 b 打印之前打印 2 和 4 打印? [英] why 2 and 4 prints before b prints?
本文介绍了为什么在 b 打印之前打印 2 和 4 打印?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
function first() {
return new Promise(resolve => {
console.log(2);
resolve(3);
console.log(4);
});
}
async function f() {
console.log(1);
let r = await first();
console.log(r);
console.log(99);
}
console.log('a');
f();
console.log('b');
在上面的代码中显示如下结果:
In the above code shows the following result:
a
1
2
4
b
3
99
据我了解,当编译器遇到await first()
函数时,会将first()
函数的执行推入事件队列并暂停f()
,继续执行f()
之后的一切,所以执行顺序应该是:
In my understanding, when the compiler hits the await first()
function, it pushes the first()
function execution to the event queue and pause the execution of f()
, continue execution everything after f()
.So the execution order should be:
a
1
b
2
4
3
99
显然,我理解错了.谁能向我解释这是如何真正起作用的?
Apparently, I get it wrong. Can anyone explain to me how this is really working?
推荐答案
如果你不明白,请发表评论.谢谢.
if you didn't understand ,leave a comment . Thank you.
这篇关于为什么在 b 打印之前打印 2 和 4 打印?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文