JavaScript为什么在外部settimeout之后运行诺言的内部定居点
运行以下代码段时,它会输出2,1
。由于Promise是一项微型任务,并且承诺内的所有内容都应在宏观任务(SetteMeout)之前运行,因此我希望输出为1,2
。因此,即使微型任务中有宏观任务,我也认为输出将为1,2
。
但是它输出2,1
。
这里有什么收获?为什么它输出2,1
而不是1,2
?
Promise.resolve().then(() => {
setTimeout(() =>{
console.log("1")
}, 0)
})
setTimeout(() => {
console.log("2")
}, 0)
When run the below code snippet, it outputs 2,1
. Since Promise is a micro-task and everything inside a promise should run before a macro-task (setTimeout), I expect that the output will be 1,2
. So even if there is a macro-task inside a micro-task, I thought the output will be 1,2
.
But it outputs 2,1
.
What's the catch here? Why does it outputs 2,1
instead 1,2
?
Promise.resolve().then(() => {
setTimeout(() =>{
console.log("1")
}, 0)
})
setTimeout(() => {
console.log("2")
}, 0)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
承诺是一项微型任务,将在
timeout1
之前被执行,但是timeout1
已经安排为宏任务。当承诺解答后,
timeout2
将被安排,但是Macrotask Q已经具有timeout1
,因此已经安排了,并将首先执行。The promise is a micro task and will get executed before
timeout1
, buttimeout1
is already scheduled as a macro task.When the promise resolves,
timeout2
will get scheduled, but the macrotask Q already hastimeout1
and as such this is already scheduled and will get executed first.这是Eventloop(我见过的最佳解释): https:// https:// https://ccol7mc4mc4mc4mc4pl0?t? = 487
剪切可能会帮助您了解它的
promis.resolve()。然后()与settimout(0)相同
因此,您基本上在timout中写了一个超时,因此Eventloop必须循环两次
This is the Eventloop (best Explenation I have ever seen): https://youtu.be/cCOL7MC4Pl0?t=487
The snipped might help you understand it
Promis.resolve().then() does the same as setTimout(0)
so you basically write a timeout inside a timout and thus the eventloop has to loop over it twice
将以下任务添加到Microtask队列中。让我们称其为“ micro1”:
任务队列看起来像
安排以下宏观施法。让我们称其为“ Macro1”
任务队列看起来像:
代码完成执行。事件循环选择下一个任务。 Microtask队列具有优先级,下一个要执行的任务是Micro1。
安排以下大型射击。让我们称其为“ macro2”
超时为零,因此无需等待。队列首先是首先,因此,任务Macro2被添加到末端。没有更高的优先级仅仅是因为它来自微型箱或类似的东西。
任务队列看起来像:
任务完成执行。事件循环选择下一个任务。 MicroTask队列是空的,因此它从Macrotask队列中挑选任务。执行的下一个任务是Macro1。
任务打印
“ 2”
到控制台。任务完成执行。事件循环选择下一个任务。 MicroTask队列是空的,因此它从Macrotask队列中挑选任务。执行的下一个任务是Macro2。
任务打印
“ 1”
到控制台。The following task is added to the microtask queue. Let's call it "micro1":
The task queues look like
The following macrotask is scheduled. Let's call it "macro1"
The task queues look like:
The code finishes executing. The event loop picks the next task. The Microtask queue has a priority, the next task to execute is micro1.
The following macrotask is scheduled. Let's call it "macro2"
The timeout is zero, therefore there is nothing to wait. The queue is first in, first out, therefore the task macro2 is added to the end. There is no higher priority just because it came from a microtask or anything like that.
The task queues look like:
Task finishes executing. The event loop picks the next task. The Microtask queue is empty, therefore it picks a task from the macrotask queue. Next task to execute is macro1.
The task prints
"2"
to the console.Task finishes executing. The event loop picks the next task. The Microtask queue is empty, therefore it picks a task from the macrotask queue. Next task to execute is macro2.
The task prints
"1"
to the console.