js中的微任务和宏任务


  • 微任务 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

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM