or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-next-tick

Environment agnostic nextTick polyfill for scheduling callbacks in the next iteration of the event loop

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/next-tick@1.1.x

To install, run

npx @tessl/cli install tessl/npm-next-tick@1.1.0

index.mddocs/

next-tick

next-tick is an environment-agnostic nextTick polyfill that enables developers to schedule callback functions to execute asynchronously in the next iteration of the event loop. It intelligently detects and uses the most appropriate asynchronous execution mechanism available in the current environment for maximum compatibility and performance across Node.js, browsers, and other JavaScript environments.

Package Information

  • Package Name: next-tick
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install next-tick

Core Imports

const nextTick = require("next-tick");
// nextTick is either a function or null depending on environment support

For environments that support ES modules:

import nextTick from "next-tick";
// nextTick is either a function or null depending on environment support

Basic Usage

const nextTick = require("next-tick");

// Check if nextTick is supported before using
if (nextTick !== null) {
  // Schedule callback for next tick
  nextTick(() => {
    console.log("This runs in the next tick");
  });
} else {
  console.log("nextTick not supported in this environment");
}

console.log("This runs immediately");

// Output (in supported environments):
// This runs immediately
// This runs in the next tick

Architecture

next-tick uses intelligent environment detection to select the optimal asynchronous execution mechanism:

  1. Node.js: Uses native process.nextTick for maximum performance
  2. Modern Engines: Uses queueMicrotask for microtask queue scheduling
  3. Browser Fallback: Uses MutationObserver for efficient DOM-based scheduling
  4. Legacy Browsers: Uses WebKitMutationObserver for older WebKit browsers
  5. Alternative Environments: Uses setImmediate when available
  6. Universal Fallback: Uses setTimeout(callback, 0) as last resort
  7. Unsupported: Returns null if no mechanisms are available

Capabilities

Asynchronous Callback Scheduling

Schedules a callback function to execute in the next iteration of the event loop using the most appropriate mechanism for the current environment. In unsupported environments, the module exports null instead of a function.

/**
 * Environment agnostic nextTick polyfill - can be null in unsupported environments
 * @type {Function|null}
 */
const nextTick = require("next-tick");

/**
 * Schedules callback for next tick execution (when nextTick is not null)
 * @param {Function} callback - The function to execute in the next tick
 * @returns {undefined} Returns undefined
 * @throws {TypeError} Throws TypeError if callback is not a function
 */
nextTick(callback);

Parameters:

  • callback (Function): The function to be executed asynchronously in the next tick. Must be a callable function.

Return Value:

  • Returns undefined

Error Handling:

  • Throws TypeError if the provided callback is not a function
  • Error message format varies by environment:
    • Node.js: Uses process.nextTick error format
    • Other environments: "[value] is not a function"

Behavior:

  • Environment Detection: The module exports null in unsupported environments instead of a function
  • Validation: When not null, validates that the callback is a function before scheduling
  • Execution Order: Executes callbacks in FIFO (first-in, first-out) order when multiple callbacks are scheduled
  • Asynchronous: Does not execute the callback immediately (asynchronous execution)
  • Performance: Uses the most performant available mechanism for the current environment

Usage Examples:

const nextTick = require("next-tick");

// Basic usage
nextTick(() => {
  console.log("Executed in next tick");
});

// Multiple callbacks execute in order
nextTick(() => console.log("First"));
nextTick(() => console.log("Second"));
nextTick(() => console.log("Third"));
// Output: First, Second, Third

// Error handling
try {
  nextTick("not a function");
} catch (error) {
  console.log(error.message); // "not a function is not a function"
}

// Environment compatibility
if (nextTick === null) {
  console.log("No nextTick mechanism available in this environment");
} else {
  nextTick(() => console.log("Supported environment"));
}

Environment Compatibility

  • Node.js: Full support using process.nextTick
  • Modern Browsers: Full support using queueMicrotask
  • Legacy Browsers: Full support using MutationObserver/WebKitMutationObserver
  • Alternative JavaScript Engines: Support using setImmediate or setTimeout
  • Unsupported Environments: Returns null instead of a function