Trusted answers to developer questions

What is clearImmediate() in Node.js?

Free System Design Interview Course

Many candidates are rejected or down-leveled due to poor performance in their System Design Interview. Stand out in System Design Interviews and get hired in 2024 with this popular free course.

The clearImmediate function is used to clear the function call scheduled by the setImmediate function. Both these functions are found in the Timers module of Node.js.

Parameters

clearInterval(timerIdentifier);

The function takes in one parameter, i.e., timeIdentifier. This is a Timeout object which is returned by SetImmediate.

Example 1

console.log("Before the setImmediate call")
let timerID = setImmediate(() => {console.log("Hello, World")});
console.log("After the setImmediate call")
clearImmediate(timerID);

In the above example, on line 2, an arrow function has been scheduled using the setImmediate function. On line 4, this scheduling was revoked by the clearImmediate function.

Example 2

function myFunction(platform){
console.log("Hi, Welcome to " + platform);
}
console.log("Before the setImmediate call")
let timerID = setImmediate(myFunction, "Educative");
console.log("After the setImmediate call")
for(let i=0; i<10; i++){
console.log("Iteration of loop: "+i);
}
clearImmediate(timerID)

In the above example, the function call is scheduled by setImmediate in line 6. This scheduling was revoked by the clearImmediate function call in line 11. Therefore, the function myFunction is never executed.

RELATED TAGS

node.js

CONTRIBUTOR

Amaaz Ahmad
Copyright ©2024 Educative, Inc. All rights reserved
Did you find this helpful?