Tags

  • AWS (7)
  • Apigee (3)
  • ArchLinux (5)
  • Array (6)
  • Backtracking (6)
  • BinarySearch (6)
  • C++ (19)
  • CI&CD (3)
  • Calculus (2)
  • DesignPattern (43)
  • DisasterRecovery (1)
  • Docker (8)
  • DynamicProgramming (20)
  • FileSystem (11)
  • Frontend (2)
  • FunctionalProgramming (1)
  • GCP (1)
  • Gentoo (6)
  • Git (15)
  • Golang (1)
  • Graph (10)
  • GraphQL (1)
  • Hardware (1)
  • Hash (1)
  • Kafka (1)
  • LinkedList (13)
  • Linux (27)
  • Lodash (2)
  • MacOS (3)
  • Makefile (1)
  • Map (5)
  • MathHistory (1)
  • MySQL (21)
  • Neovim (10)
  • Network (66)
  • Nginx (6)
  • Node.js (33)
  • OpenGL (6)
  • PriorityQueue (1)
  • ProgrammingLanguage (9)
  • Python (10)
  • RealAnalysis (20)
  • Recursion (3)
  • Redis (1)
  • RegularExpression (1)
  • Ruby (19)
  • SQLite (1)
  • Sentry (3)
  • Set (4)
  • Shell (3)
  • SoftwareEngineering (12)
  • Sorting (2)
  • Stack (4)
  • String (2)
  • SystemDesign (13)
  • Terraform (2)
  • Tree (24)
  • Trie (2)
  • TwoPointers (16)
  • TypeScript (3)
  • Ubuntu (4)
  • Home

    process.nextTick() & setImmediate()

    Published Oct 04, 2019 [  Node.js  ]

    What is Node.js Event Loop?

    Due to the fact that JavaScript is single-threaded i.e. it performs only a single process at a time, so it is the Event Loop that allows Node.js to perform non-blocking I/O operations.

    At the start of a JavaScript program, an event loop is initialized. There are several operations that execute in an event loop. Below is the sequence in which different they are executed in a single iteration of the event loop. These operations are processed in a queue.

    Phases of Event Loop in Node.js

    process.nextTick()

    1

    Whenever a new queue of operations is initialized we can think of it as a new tick. The process.nextTick() method adds the callback function to the start of the next event queue. It is to be noted that, at the start of the program process.nextTick() method is called for the first time before the event loop is processed.

    2

    When in Node.js, one iteration of the event loop is completed. This is known as a tick. process.nextTick() taking a callback function which is executed after completing the current iteration/tick of the event loop.

    process.nextTick() schedule a callback function to be execute in next iteration of event loop.

    When we use process.nextTick()

    1. To cleanup unwanted resources.
    2. To allow users to handle errors.
    3. To try a request to run before starting the next iteration of event loop.
    4. Allow a callback to run after call stack and before next iteration of event loop.

    setImmediate()

    Whenever we call setImmediate() method, it’s callback function is placed in the check phase of the next event queue. There is slight detail to be noted here that setImmediate() method is called in the poll phase and it’s callback functions are invoked in the check phase.

    Example

    setImmediate(function A() {
    	console.log("1st immediate");
    });
    
    setImmediate(function B() {
    	console.log("2nd immediate");
    });
    
    process.nextTick(function C() {
    	console.log("1st process");
    });
    
    process.nextTick(function D() {
    	console.log("2nd process");
    });
    
    // First event queue ends here
    console.log("program started");
    

    Output

    program started
    1st process
    2nd process
    1st immediate
    2nd immediate
    

    Explanation

    For the above program, event queues are initialized in the following manner:

    1. In the first event queue only ‘program started is printed’.
    2. Then second event queue is started and function C i.e. callback of process.nextTick() method is placed at the start of the event queue. C is executed and the queue ends.
    3. Then previous event queue ends and third event queue is initialized with callback D. Then callback function A of setImmdeiate() method is placed in the followed by B. Now, the third event queue looks like this,
    D A B
    

    Now functions D, A, B are executed in the order they are present in the queue.

    References: