zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    Alpha冲刺(1/4)
    团队项目用户验收评审
    beta冲刺 第四天
    beta冲刺 第三天
    Beta冲刺 第二天
    Beta冲刺 第一天
    实验十一 团队作业7---团队项目设计完善&编码测试
    Alpha冲刺四
    Alpha冲刺
    《Miracle_House》团队项目系统设计改进
  • 原文地址:https://www.cnblogs.com/shewill/p/13051359.html
Copyright © 2011-2022 走看看