Difference setTimeout, setInterval, and setImmediate

1

What is the difference between setInterval , setTimeout and setImmediate in Node.JS?

I know that setTimeout creates a single timer, like a delay, and setInterval creates a range. But what about setImmediate ?

    
asked by anonymous 22.04.2018 / 15:47

1 answer

1

According to the documentation :

setTimeout() can be used to schedule code execution after a designated number of milliseconds. This function is similar to window.setTimeout() of the browser's JavaScript API, however, a code string can not be passed to execute.

setImmediate() will execute the code at the end of the cycle of the current event cycle. This code will be executed after any I / O operation in the current event loop and before any timers scheduled for the next event loop. This code execution can be thought of as occurring "right after", meaning that any code after the setImmediate() function call is executed before the setImmediate() function argument.

If there is a block of code that must be executed multiple times, setInterval() can be used to execute this code. setInterval() receives a function argument that will be executed an infinite number of times with a given millisecond delay as the second argument. Like setTimeout() , additional arguments can be added beyond the delay, and these will be passed to the function call.

At a glance in this video of rodrigo branas about node.js timers

    
22.04.2018 / 17:07