We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
浏览器的执行循环顺序:
Node使用libuv(实现 Node.js 事件循环和平台的所有异步行为的 C 函数库)暴露的API实现事件循环机制
poll 轮询阶段流程:
Node:microtask 在事件循环的各个阶段之间执行 浏览器:microtask 在事件循环的 macrotask 执行完之后执行
setTimeout(()=>{ console.log('timer1') Promise.resolve().then(function() { console.log('promise1') }) }, 0) setTimeout(()=>{ console.log('timer2') Promise.resolve().then(function() { console.log('promise2') }) }, 0)
浏览器端运行结果:timer1=>promise1=>timer2=>promise2 Node11之前的版本:timer1=>promise1=>timer2=>promise2(timers阶段会依次执行完) Node11之后的版本:timer1=>promise1=>timer2=>promise2(执行一次宏任务后立即执行微任务)
async function async1(){ console.log('async1 start') await async2() console.log('async1 end') } async function async2(){ console.log('async2') } console.log('script start') setTimeout(function(){ console.log('setTimeout0') },0) setTimeout(function(){ console.log('setTimeout3') },3) setImmediate(() => console.log('setImmediate')); process.nextTick(() => console.log('nextTick')); async1(); new Promise(function(resolve){ console.log('promise1') resolve(); console.log('promise2') }).then(function(){ console.log('promise3') }) console.log('script end') // 正确结果 script start async1 start async2 promise1 promise2 script end nextTick async1 end promise3 setTimeout0 setImmediate setTimeout3
浏览器与Node的事件循环(Event Loop)有何区别? 一道面试题引发的node事件循环深入思考
The text was updated successfully, but these errors were encountered:
sihai00
No branches or pull requests
浏览器
浏览器的执行循环顺序:
Node
Node使用libuv(实现 Node.js 事件循环和平台的所有异步行为的 C 函数库)暴露的API实现事件循环机制
poll 轮询阶段流程:
区别
Node:microtask 在事件循环的各个阶段之间执行
浏览器:microtask 在事件循环的 macrotask 执行完之后执行
特例:Node11前后版本差异
浏览器端运行结果:timer1=>promise1=>timer2=>promise2
Node11之前的版本:timer1=>promise1=>timer2=>promise2(timers阶段会依次执行完)
Node11之后的版本:timer1=>promise1=>timer2=>promise2(执行一次宏任务后立即执行微任务)
题目
参考
浏览器与Node的事件循环(Event Loop)有何区别?
一道面试题引发的node事件循环深入思考
The text was updated successfully, but these errors were encountered: