你以点击或通过该守则加以操作的方式看待不同结果的原因,是合成业务是如何运作的,特别是许诺。
When you manually click the screen, the event listeners attached to the click event of the body element are triggered in the order they were added in your code.
In this case, the first listener executes, logs 2, then 1, and then the second listener executes, logging 4 and 3 respectively.
2
1
4
3
Running document.body.click():
When you execute document.body.click(), the click event is triggered programmatically, but since you re also using promises (Promise.resolve().then(...)), the behavior slightly differs.
The event listeners attached to the click event are still executed in the order they were added.
However, due to the promises, the callbacks inside them (logging 1, 2, 3, 4) might not execute immediately, as they are queued in the microtask queue.
This means that while the click event is fired immediately when you call document.body.click(), the promises inside the event listeners are resolved asynchronously, and their callbacks (console.log statements) are executed later.
So, the asynchronous nature of promises causes the difference in timing between manual and programmatic clicks, resulting in the different order of logged numbers.
我建议检查:microtasks and pledges。