大佬们,小弟最近在学习 Node.js ,发现 Promise.then 中的代码会先于 process.nextTick 执行,网上资料普遍说的是 process.nextTick 会先于微任务执行,请问这是什么原因...
有如下代码:
console.log("script start"); setTimeout(() => { console.log("setTimeout"); }, 0); process.nextTick(() => console.log("nextTick")); new Promise((resolve, reject) => { console.log("promise1"); resolve(undefined); console.log("promise2"); }).then(() => { console.log("promise3"); }); console.log("script end"); 执行结果为:
script start promise1 promise2 script end promise3 // 为什么会先于输出这个而不是 nextTick ?? nextTick setTimeout 直接使用 Node.js 执行 ts 文件,代码执行环境: Node:v25.2.1 TypeScript:5.9.3
tsconfig.json
{ "compilerOptions": { "target": "ESNext", "module": "ESNext", "esModuleInterop": true, "forceConsistentCasingInFileNames": true, "strict": true, "skipLibCheck": true, "noImplicitAny": true, "noImplicitReturns": true, "strictNullChecks": true } } 
