Environment agnostic nextTick polyfill for scheduling callbacks in the next iteration of the event loop
npx @tessl/cli install tessl/npm-next-tick@1.1.0next-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.
npm install next-tickconst nextTick = require("next-tick");
// nextTick is either a function or null depending on environment supportFor environments that support ES modules:
import nextTick from "next-tick";
// nextTick is either a function or null depending on environment supportconst 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 ticknext-tick uses intelligent environment detection to select the optimal asynchronous execution mechanism:
process.nextTick for maximum performancequeueMicrotask for microtask queue schedulingMutationObserver for efficient DOM-based schedulingWebKitMutationObserver for older WebKit browserssetImmediate when availablesetTimeout(callback, 0) as last resortnull if no mechanisms are availableSchedules 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:
undefinedError Handling:
TypeError if the provided callback is not a functionprocess.nextTick error format"[value] is not a function"Behavior:
null in unsupported environments instead of a functionUsage 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"));
}process.nextTickqueueMicrotaskMutationObserver/WebKitMutationObserversetImmediate or setTimeoutnull instead of a function