以下运行代码将获得:
c,a,d,b
// case 1
const test = async () => {
new Promise((resolve) => {
resolve('a');
}).then(r => console.log(r));
process.nextTick(() => {
console.log('c');
});
console.log(await 'd');
return 'b';
};
(async () => {
const r = await test();
console.log(r);
})();
但是,如果我把过程。下一个复选框
await 'd'
// case 2
const test = async () => {
new Promise((resolve) => {
resolve('a');
}).then(r => console.log(r));
console.log(await 'd');
process.nextTick(() => {
console.log('c');
});
new Promise((resolve) => {
resolve('e');
}).then(r => console.log(r));
return 'b';
};
(async () => {
const r = await test();
console.log(r);
})();
我得到:
a,d,e,b,c
为什么选择流程。nextTick的执行顺序是这样变化的?
根据我有限的知识,
nextTick
之前运行
Promise.resolve()
如所示
case1
,所以我希望
a,d,c,e,b
而不是
a、 d、e、b、c
在里面
case2