Read the big guy about Promise this article talk about their feelings juejin.cn/post/694531…

On the first code

//pp1
Promise.resolve().then(() = > {
    console.log(0);/ / 1
    return Promise.resolve(4);//temp2 completes without then
}).then((res) = > {
    console.log(res)/ / 5
})
//pp2
Promise.resolve().then(() = > {
    console.log(1);/ / 2
}).then(() = > {
    console.log(2);/ / 3
}).then(() = > {
    console.log(3);/ / 4
}).then(() = > {
    console.log(5);/ / 6
}).then(() = >{
    console.log(6);/ / 7
})

Copy the code

surprise 0 1 2 3 4 5 6

Another big articles from https://juejin.cn/post/6844903987183894535#heading-2

Several knowledge points are extracted

The registration of the external second THEN needs to wait for the synchronization code execution of the external first THEN to complete

The event mechanism is “register first execute”

Let’s start by sharing my insights. Two promises are named PP1 pp2

1. Register the function after pp1.resolve()

2. Register the function after pp2.resolve()

Resolve (4) At this point, the Promis will enter the microtask queue as temp2

4. Execute tag 2 to register tag 3

5. Then execute the Promise of resolve with the marker temp2 RETURN. After the execution is complete, register the next.then but there is no.then inside so. The first PP1 THEN synchronization task is completed

6. Perform tag 3 to register tag 4

7. Register tag 5 for temp2 to complete

8. Perform tag 4 to register tag 6

9. Execute tag 5

10. Perform tag 6 to register tag 7

The overall order is marked:

1 temp2(no THEN but also registration action) 3 4 5 6 7

Refer to the article

Juejin. Cn/post / 694531… https://juejin.cn/post/6844903987183894535#heading-2

If there is any mistake, please correct it