I did a series of tests on this question
wrote an article (note address below)
the task mechanism of Javascript under https://codeshelper.com/a/1190000014665591">chrome https://api-v5.segmentfault.com/question/
three pieces of code have been tested in the article
above. Here I will post a paragraph: https://api-v5.segmentfault.com/question/
console.log("start");
const interval = setInterval(() => {
console.log("setInterval");
}, 0);
setTimeout(() => {
console.log("setTimeout 1");
Promise.resolve()
.then(() => {
console.log("promise 3");
})
.then(() => {
console.log("promise 4");
})
.then(() => {
setTimeout(() => {
console.log("setTimeout 2");
Promise.resolve()
.then(() => {
console.log("promise 5");
})
.then(() => {
console.log("promise 6");
})
.then(() => {
clearInterval(interval);
});
}, 0);
});
}, 0);
Promise.resolve()
.then(() => {
console.log("promise 1");
})
.then(() => {
console.log("promise 2");
});
https://api-v5.segmentfault.com/question/
according to the event mechanism known by the author
the output should be as follows: https://api-v5.segmentfault.com/question/
start
promise 1
promise 2
setInterval
setTimeout 1
promise 3
promise 4
setInterval
setTimeout 2
promise 5
promise 6
https://api-v5.segmentfault.com/question/
this result is proved to be correct in mac/windows firefox, mac safari, windows edge, mac/windows opera https://api-v5.segmentfault.com/question/
but in windows/mac chrome (the window/mac chrome version used for testing is Version 65.0.3325.181 (Official Build) (64-bit), there is a high probability that another result will occur: https://api-v5.segmentfault.com/question/
start
promise 1
promise 2
setInterval
setTimeout 1
promise 3
promise 4
setInterval
setInterval
setTimeout 2
promise 5
promise 6
https://api-v5.segmentfault.com/question/
excuse me, why is this?
there are some other test code in the article, so I won"t list them here. https://api-v5.segmentfault.com/question/