Promise console.log All In One_console.logPromise console.log All In One 同步事件/异步事件 微任务/宏任务



Promise console.log All In One

同步事件/异步事件

微任务/宏任务

js 事件循环原理

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

Promise console.log All In One_console.log


"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

*/