相关疑难解决方法(0)

事件循环和Promise之间有什么关系

我很好奇Event Loop和Promise之间的关系.
该演示揭示了这个问题.我希望它p1 fulfilled出现在中间,因为它们将任务排队到同一个任务队列并逐个执行.

var p1 = new Promise(function(resolve, reject){
    resolve(1)
})
setTimeout(function(){
  console.log("will be executed at the top of the next Event Loop")
},0)
p1.then(function(value){
  console.log("p1 fulfilled")
})
setTimeout(function(){
  console.log("will be executed at the bottom of the next Event Loop")
},0)
Run Code Online (Sandbox Code Playgroud)

控制台结果是:

p1 fulfilled
will be executed at the top of the next Event Loop
will be executed at the bottom of the next Event Loop
Run Code Online (Sandbox Code Playgroud)

可视化效果显示promise.then回调没有进入事件循环的任务队列.这是正确的?

【注意:问题与Promise vs setTimeout不一样,因为它更关注Event Loop和Promise之间的关系】

javascript theory event-loop promise

12
推荐指数
1
解决办法
5478
查看次数

标签 统计

event-loop ×1

javascript ×1

promise ×1

theory ×1