Give yourself a problem, directly on the code

new Promise((resolve) => {
    resolve()
}).then(() => { // then1
    console.log(1)
    Promise.resolve()
        .then(() => { // then2
            console.log(2)
        })
        .then(() => { // then3
            console.log(3)
            Promise.resolve()
                .then(() => { // then4
                    console.log(4)
                }).then(() => { // then5
                    console.log(5)
                })
        }).then(() => { // then6
            console.log(6)
        })
}).then(() => { // then7
    console.log(7)
}).then(() => { // then8
    console.log(8)
    Promise.resolve()
        .then(() => { // then9
            console.log(9)
        }).then(() => { // then10
            console.log(10)
        })
}).then(() => { // then11
    console.log(11)
})
Copy the code

The execution process is as follows

// Queue first in, first out: ->[] Then -> // The chain call will continue after the synchronous code in then completes execution, // -> [THEN1] // THEN1 -> Execute the synchronization code print 1 Put THEN2 into the queue after then1 put then7 into the queue // -> [THEN7,then2] // Then2 -> [then3, // then7 -> [then8, Then3] / / then3 - > perform synchronization code print 3 defines a new promise put then4 in queue then3 of synchronization code execution is completed / / put behind then3 then6 in the queue - > [then6 then4, Then8] / / then8 - > perform synchronization code print 8 defines a new promise will be finished then9 into the queue of then8 synchronization code execution / / put behind then8 then11 in the queue - > [then11 then9, Then6 then4] / / then4 - > perform synchronization code to print in 4 then4 synchronization code execution is completed Put then5 in queue / / - > [then5 then11, then9, Then9 -> execute then9 -> execute then9 -> execute then10 -> queue then10 -> [THEN10, then5, Then11] // -> THEN11 -> Execute the synchronization code print 11 The synchronization code in THEN11 completes // -> THEN5 -> execute the synchronization code print 5 the synchronization code in THEN5 completes // -> THEN10 -> execute the synchronization code print Print 10 Then10 the synchronization code in then10 is completeCopy the code

The result is as follows

> 1

> 2

> 7

> 3

> 8

> 4

> 6

> 9

> 11

> 5

> 10