博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

node 笔记:event loop

Posted on 2022-06-03 09:10  pencilCool  阅读(17)  评论(0编辑  收藏  举报

参考 :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