Open In App

Node.js Immediate Timer Class

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

The timer module is used for scheduling functions which will be called at some future period of time. It is a global API So, there is no need to import (require(“timers”)) to use it.

Immediate Class has an object (setImmediate()) which is created internally to scheduled actions, and (clearImmediate()) can be passed in order to cancel those scheduled actions. When an immediate is scheduled, By default, the event loop of Node.js will continue running until clearImmediate() is called. The setImmediate() Method returns immediate objects which are used to control this default behavior, and exports both immediate.ref() and immediate.unref() functions.

The three immediate class objects are defined below: 

immediate.hasRef(): (Added in v11.0.0) This immediate object keeps the event loop active until the returned ‘True’ if returned ‘false’ breaks the event loop.

Syntax:

immediate.hasRef()

Return Value<boolean>: This immediate object keeps the event loop active if returned ‘True’.

immediate.ref(): (Added in v9.7.0) When the Immediate is active and (immediate.ref()) called then it requests that the Node.js event loop did not exist for so long. Anyway, calling this Method multiple times does not have any effect. 

Syntax:

immediate.ref()

Return Value <Immediate>: It returns an immediate reference.

immediate.unref(): (Added in v9.7.0) When the Immediate is active it does not require the Node.js event loop to remain active. The callback of the Immediate object is invoked after the process gets exit if any other activity keeps the event loop running. Anyway, calling this Method also multiple times does not have any effect.

Syntax:

immediate.unref()

Return Value<Immediate>: It returns an immediate reference. 

Example: In this example, we will see the demonstration of Immediate Class Methods.

Filename: index.js  

Javascript




// Node.js program to demonstrate the
// Immediate Class methods
 
// Setting Immediate by setImmediate Method
let Immediate = setImmediate(function alfa() {
    console.log("0.>", 12);
});
 
// Printing Immediate.hasRef method
console.log("1.>", Immediate.hasRef());
// Returns true
 
// Printing Immediate.ref before unref
console.log("2.>", Immediate.ref());
// Returns timer reference
 
// Printing Immediate.unref method
console.log("3.>", Immediate.unref());
// Returns Immediate reference and
// sets hasRef to false
 
// Printing Immediate.hasRef before unref
console.log("4.>", Immediate.hasRef());
// Returns false
 
// Clears setInterval Immediate
clearImmediate(Immediate);
 
// Prints after clearing Immediate
console.log("5.>", 2);


Run the index.js file using the following command:

node index.js

Output:

1.> true
2.> Immediate {
  _idleNext: null, ......, [Symbol(triggerId)]: 1
}
3.> Immediate {
 _idleNext: null, ......, [Symbol(triggerId)]: 1
}
4.> false
5.> 2

Reference: https://nodejs.org/api/timers.html#timers_class_immediate


Last Updated : 11 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads