参考 :https://nodejs.dev/learn/the-nodejs-event-loop
Message Queue : 老同志了。click event ,setTimeout,这些 就是他默默在支持,性子比较平和,别人插队他就默默等着
Job Queue / ES6 Job Queue: 新人,时髦的Promise 就是他的杰作,性子急躁,不许别人插队。
const bar = () => console.log('bar')
const baz = () => console.log('baz')
const foo = () => {
console.log('foo')
setTimeout(bar, 0)
baz()
}
foo()
terminal:
foo
baz
bar
const bar = () => console.log('bar')
const baz = () => console.log('baz')
const foo = () => {
console.log('foo')
setTimeout(bar, 0)
new Promise((resolve, reject) =>
{
console.log("before resolve")
resolve('should be right after baz, before bar')
console.log("after resolve")
}
).then(resolve => console.log(resolve))
baz()
}
foo()
terminal:
foo
before resolve
after resolve
baz
should be right after baz, before bar
bar