码迷,mamicode.com
首页 > Web开发 > 详细

js中的微任务和宏任务

时间:2020-06-05 19:33:43      阅读:104      评论:0      收藏:0      [点我收藏+]

标签:创建   color   sync   语句块   block   col   val   ons   style   

  • 微任务 promise 、async await

    • 微任务 遇到微任务,放在当前任务列的最底端(then或者catch里面的内容)

  • 宏任务 setTimeout setInterval

    • 宏任务 遇到宏任务,放到下一个新增任务列的最顶端

  • 当前任务列执行完成了再去执行下一个任务列

    • 当then和then的外层都有宏任务时,先创建外层的宏任务

Promise.resolve().then(function () {
        console.log(1);
        Promise.resolve().then(function () {
          console.log(2);
        });
        Promise.resolve().then(function(){
            console.log(3);
        });
      });
      Promise.resolve().then(function(){
          console.log(4);
      })//1423

    async function fn(){
          console.log(1);
          await Promise.resolve().then(function(){
              console.log(2);
          })
          await Promise.resolve().then(function(){
              return 3;
          });
          await Promise.resolve().then(function(){
              console.log(4);
          })
      }
      fn().then(function(num){
          console.log(num);
      })//124underfined

      console.log(0);
      setTimeout(console.log(1),0);//这个里面执行的是代码块不是语句块,直接执行就OK了
      console.log(2);
      //012

 

js中的微任务和宏任务

标签:创建   color   sync   语句块   block   col   val   ons   style   

原文地址:https://www.cnblogs.com/shewill/p/13051359.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!