Timers on Node.js: their differences and their relationship to process.nextTick

Asked

Viewed 854 times

1

What’s the difference between setInterval, setTimeout and setImmediate in Node.js?

I know that setTimeout creates a single timer, such as a delay, and the setInterval creates a gap. But setImmediate?

In addition, there is a relationship between these timers and the process.nextTick?

1 answer

1

According to the documentation:

setTimeout() can be used to schedule code execution after a designated amount of milliseconds. This function is similar to window.setTimeout() of the browser’s Javascript API, however, a code string cannot be passed to be executed.

setImmediate() will execute the code at the end of the current event cycle 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 "soon after", meaning that any code after the function call setImmediate() will be executed before the function argument setImmediate().

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

At a glance in that video from White Rodrigo on Node.js timers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.