AI Workshop: learn to build apps with AI →
Core Concepts: Understanding setImmediate()

Join the AI Workshop and learn to build real-world apps with AI. A hands-on, practical program to level up your skills.


When you want to execute some piece of code asynchronously, but as soon as possible, one option is to use the setImmediate() function provided by Node.js:

setImmediate(() => {
  // run something
})

The function passed to setImmediate() is a callback that runs in the next iteration of the event loop.

How is setImmediate() different from setTimeout(() => {}, 0) (passing a 0ms timeout), and from process.nextTick()?

A function passed to process.nextTick() runs on the current iteration of the event loop, after the current operation ends. It therefore runs before setTimeout and setImmediate.

A setTimeout() callback with a 0ms delay is very similar to setImmediate(). The execution order will depend on various factors, but they will be both run in the next iteration of the event loop.

Lessons in this unit:

0: Introduction
1: The Node Core Modules
2: Differences between Node and the Browser
3: Node, the difference between development and production
4: The Node.js Event Loop
5: A brief history of Node.js
6: Understanding process.nextTick()
7: ▶︎ Understanding setImmediate()
8: How to check the current Node.js version at runtime
9: Why should you use Node.js in your next project?
10: Introduction to Node.js