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
  • 相关阅读:
    JavaScript 实现深度拷贝
    JacaScript arguments
    EMACS 使用入门
    ubuntu 14.04 nginx + mysql + php源码安装
    c语言 头文件
    程序员技术练级攻略
    if和switch的选择
    .htaccess (分布式配置文件)
    yii2 windows 安装过程
    Js 冒泡事件阻止
  • 原文地址:https://www.cnblogs.com/shewill/p/13051359.html
Copyright © 2011-2022 走看看