Skip to main content
Process.nextTick - node__process.d.ts - Node documentation
method Process.nextTick

Usage in Deno

```typescript import { type Process } from "node:node__process.d.ts"; ```
Process.nextTick(
callback: Function,
...args: any[],
): void
`process.nextTick()` adds `callback` to the "next tick queue". This queue is fully drained after the current operation on the JavaScript stack runs to completion and before the event loop is allowed to continue. It's possible to create an infinite loop if one were to recursively call `process.nextTick()`. See the [Event Loop](https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/#process-nexttick) guide for more background. ```js import { nextTick } from 'node:process'; console.log('start'); nextTick(() => { console.log('nextTick callback'); }); console.log('scheduled'); // Output: // start // scheduled // nextTick callback ``` This is important when developing APIs in order to give users the opportunity to assign event handlers _after_ an object has been constructed but before any I/O has occurred: ```js import { nextTick } from 'node:process'; function MyThing(options) { this.setupOptions(options); nextTick(() => { this.startDoingStuff(); }); } const thing = new MyThing(); thing.getReadyForStuff(); // thing.startDoingStuff() gets called now, not before. ``` It is very important for APIs to be either 100% synchronous or 100% asynchronous. Consider this example: ```js // WARNING! DO NOT USE! BAD UNSAFE HAZARD! function maybeSync(arg, cb) { if (arg) { cb(); return; } fs.stat('file', cb); } ``` This API is hazardous because in the following case: ```js const maybeTrue = Math.random() > 0.5; maybeSync(maybeTrue, () => { foo(); }); bar(); ``` It is not clear whether `foo()` or `bar()` will be called first. The following approach is much better: ```js import { nextTick } from 'node:process'; function definitelyAsync(arg, cb) { if (arg) { nextTick(cb); return; } fs.stat('file', cb); } ```

Parameters

callback: Function
...args: any[]

Return Type

void