xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

Promise console.log All In One

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(`\n`);

(() => {
  // 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 发布文章使用:只允许注册用户才可以访问!


posted @ 2020-11-09 22:52  xgqfrms  阅读(156)  评论(1编辑  收藏  举报