Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

node.js

What is clearImmediate() in Node.js?

Amaaz Ahmad

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 ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring