Are process.nextTick callbacks called in order?

If you call process.nextTick()multiple times, are callbacks done in order?

In other words, does the Node event loop give the same priority to all calls process.nextTickand execute them in FIFO order?

For instance:

process.nextTick(() => console.log('1'))
process.nextTick(() => console.log('2'))

process.nextTick(() =>
  process.nextTick(() => console.log('3'))
)

process.nextTick(() =>
  process.nextTick(() => console.log('4'))
)

Will this always print 1 2 3 4?

+4
source share
1 answer

Which queue is used is not written in docs , and I could not find the official source that explains this, so I looked at the source code and it seems that this is a FIFO queue, as you indicated.

, nextTickQueue nextTick() _tickCallback(), tickInfo[kIndex] while.

, ; .

, FIFO , , ; .

+2

Source: https://habr.com/ru/post/1666672/


All Articles