Using process.nextTick and setImmediate

tldr: This doc about event loop, timers, and next tick.

As any modern developer might tell you, Node JS is a single-threaded runtime that relies on an asynchronous non-blocking I/O to achieve high-performance programming. The underlying model for doing this is based on an “event loop”.

This is usually utilized for normal use cases which would call for parallel/concurrent programming, such as response handlers for an HTTP endpoint.

Sometimes, however, we also utilize Node’s async I/O to allow us to do some things with decreased priority, or without blocking the current execution context.

An abstract example:

client     server
req ->    >req
           handle req
           enqueue background job
res <-    <response
            handle backgroud job
            finish background job

So, if we want this kind behavior, what should we use? process.nextTick or setImmediate?





Last modified: August 12, 2022
© 2024