zoukankan      html  css  js  c++  java
  • xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

    Promise console.log All In One

    同步事件/异步事件

    微任务/宏任务

    js 事件循环原理

    1. 先执行 同步事件
    2. 在执行,异步事件的所有微任务队列,按照时间顺序
    3. 最后执行,异步事件里的宏任务队列,按照时间顺序

    
    "use strict";
    
    /**
     *
     * @author xgqfrms
     * @license MIT
     * @copyright xgqfrms
     * @created 2020-10-01
     * @modified
     *
     * @description
     * @difficulty Easy Medium Hard
     * @complexity O(n)
     * @augments
     * @example
     * @link
     * @solutions
     *
     * @best_solutions
     *
     */
    
    const log = console.log;
    
    
    (() => {
      const log = console.log;
      setTimeout(() => {
        log(`1`);
      }, 0);
      log(`2`);
      new Promise((resolve, reject) => {
        log(`3`);
        setTimeout(() => {
          log(`4`);
        }, 0);
        setTimeout(() => {
          log(`5`);
        }, 1);
        resolve();
        // reject();
      }).then(value => {
        log(`6`);
      }).catch(err => {
        log(`error`);
      });
      log(`7`);
    })();
    
    log(`
    `);
    
    (() => {
      // const log = console.log;
      setTimeout(() => {
        log(`11`);
      }, 0);
      log(`22`);
      new Promise((resolve, reject) => {
        log(`33`);
        setTimeout(() => {
          log(`44`);
        }, 0);
        setTimeout(() => {
          log(`55`);
        }, 1);
        // resolve();
        reject();
      }).then(value => {
        log(`66`);
      }).catch(err => {
        log(`error`);
      });
      log(`77`);
    })();
    
    /*
    
    js 事件循环原理
    
    1. 先执行 同步事件
    2. 在执行,异步事件的所有微任务队列,按照时间顺序
    3. 最后执行,异步事件里的宏任务队列,按照时间顺序
    
    2
    3
    7
    6
    1
    4
    5
    
    */
    
    
    /*
    
    2
    3
    7
    
    22
    33
    77
    6
    error
    1
    4
    5
    11
    44
    55
    
    */
    
    
    

    refs



    ©xgqfrms 2012-2020

    www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!


  • 相关阅读:
    $.ajax()、$.post()、$.get()
    Json数据格式
    iOS UIButton 调节文字和图片的位置
    block 中使用 weakSelf
    iOS 移除导航栏黑线
    Swift 设置导航栏透明
    Xcode 8 证书管理 Signing for "xxxx" requires a development team.
    iOS 中文登录 中文参数 转码
    ios apple pay 证书配置
    Missing iOS Distribution signing identity for ... Xcode can request one for you.
  • 原文地址:https://www.cnblogs.com/xgqfrms/p/13951444.html
Copyright © 2011-2022 走看看