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