console.log(1);
setTimeout(function () {
console.log(2)
},0)
new Promise(function (resolve) {
console.log(3);
resolve();
console.log(4);
}).then(function () {
console.log(5);
});
console.log(6)
console.log(1);
setTimeout(function () {
console.log(2)
},0)
new Promise(function (resolve) {
console.log(3);
resolve();
console.log(4);
}).then(function () {
console.log(5);
});
console.log(6)
1-3-4-6-5-2
1应该没有问题
2是因为setTimeout 0 会放在当前代码队列最后
3应该也是没问题,
4是因为resolve();
只是改变状态,但是不影响当前作用域下后续代码执行,
5是因为thenAction是其实也是异步的,所以会在6之后输出。
setTimeout中的代码会在下个tick中执行,Promise中的代码正常执行,then方法中的代码在当前tick的末尾执行 所以执行顺序是 tick1(1 3 4 6 5) nextTick(2)
10 回答11.2k 阅读
5 回答4.8k 阅读✓ 已解决
4 回答3.1k 阅读✓ 已解决
2 回答2.7k 阅读✓ 已解决
3 回答2.3k 阅读✓ 已解决
3 回答2.2k 阅读✓ 已解决
2 回答2.6k 阅读✓ 已解决
极其经典的一道题
输出顺序是 1,3,4,6,5,2
setTimeout()
开了个新进程,会在最后执行Promise()中
.then()
会在整体结束后执行。看看这篇文章:https://segmentfault.com/p/12...