-
Notifications
You must be signed in to change notification settings - Fork 0
/
6-el-exec-order.js
44 lines (34 loc) · 1.17 KB
/
6-el-exec-order.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
setTimeout(() => console.log("timeout-first")); // Create new macro-task
setTimeout(() => console.log("timeout-second"), 1000); // Create new macro-task
Promise.resolve().then(() => console.log("promise-first")); // Create new micro-task
// Create new micro-task
const promise = new Promise((resolve) => {
resolve();
console.log("resolve"); // sync code
});
// Exec (promise) micro-task
promise.then(() => {
console.log("promise-second"); // Sync code after after execution of promise
setTimeout(() => {
console.log("promise-timeout"); // Create new macro-task after execution of promise
});
});
setTimeout(() => console.log("timeout-third")); // Create new macro-task
queueMicrotask(() => console.log("queueMicrotask")); // Create new micro-task
// Sync code (async function without promise behave as a simple function)
async function asyncFunc() {
await console.log("asyncFunc");
}
asyncFunc(); // exec of function
console.log("code"); // sync code
/* Console log result */
// 1 - resolve
// 2 - asyncFunc
// 3 - code
// 4 - promise-first
// 5 - promise-second
// 6 - queueMicrotask
// 7 - timeout-first
// 8 - timeout-third
// 9 - promise-timeout
// 10 - timeout-second