How to use setImmediate(callback) in Async

setImmediate(callback) is a method provided by the Async module that defers a callback function’s execution until the next event loop iteration.

In Node.js, this is called setImmediate. The browser will use setImmediate if available; otherwise, setTimeout(callback, 0) will be used (this means other higher priority events may precede the execution of the callback function).

Syntax

setImmediate(callback, args...)

  • callback: The function to call on a later loop around the event loop.
  • args...: (Optional) Additional arguments to pass to the callback.

Example

In this example, we defer the execution of a function that accepts a name argument and outputs that name to the console.

Notice that the statement console.log('1. Hello') runs before the statement console.log('2.', name) inside the callback function passed to setImmediate().

import setImmediate from 'async/setImmediate';
// Defering the execution of a function.
setImmediate(function(name) {
// This will run second
console.log('2.', name);
}, 'World');
// This will run first
console.log('1. Hello');

For information on nextTick(callback), a function similar to setImmediate(callback) in the Async module, click here.

New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved