Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags


How to use nextTick(callback) in Async

Nouman Abbasi

nextTick(callback) is a method provided by the Async module that defers the execution of a function until the next event loop iteration. Similar to how the setTimeout() is used in browsers to defer execution of a function by a given time, nextTick() defers execution until the next tick.

In Node.js, each iteration of an event loop is called a tick.

In Node.js, nextTick() just calls process.nextTick. In the browser, it will use setImmediate if available, otherwise it will use setTimeout(callback, 0), which means that the other higher priority events may precede the execution of the callback function.


The function signature is as follows:

nextTick(callback, arg...)

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


In this example, we simply defer the execution of a function that accepts a name argument and output 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 nextTick().

import nextTick from 'async/nextTick';

// Defering the execution of a function.
nextTick(function(name) {
    // This will run second
    console.log('2.', name);
}, 'World');

// This will run first
console.log('1. Hello');




Nouman Abbasi
Copyright ©2022 Educative, Inc. All rights reserved

View all Courses

Keep Exploring