understanding the node.js event queue and process.nextTick

后端 未结 2 1007
清歌不尽
清歌不尽 2020-12-16 05:16

I\'m having trouble understanding exactly how process.nextTick does its thing. I thought I understood, but I can\'t seem to replicate how I feel this should wor

相关标签:
2条回答
  • 2020-12-16 05:37

    process.nextTick put the callback on the next tick that is going to be executed, not at the end of the tick queue.

    Node.js doc (http://nodejs.org/api/process.html#process_process_nexttick_callback) say: "It typically runs before any other I/O events fire, but there are some exceptions."

    setTimeout(callback, 0) will probably work more like you describe.

    0 讨论(0)
  • 2020-12-16 05:43

    You should certainly read the link fgascon provided, and perhaps

    https://github.com/joyent/node/issues/3335 for more background.

    Use process.nextTick for when you want to call some code before any IO, but after the calling context has returned (usually because you want to register listeners on an event emitter and need to return the created emitter before you can register anything).

    0 讨论(0)
提交回复
热议问题