Isomorphic Timers Promises

1.0.1 · active · verified Sun Apr 19

The `isomorphic-timers-promises` package provides a unified, promise-based API for handling asynchronous timers (`setTimeout`, `setImmediate`, `setInterval`) across both client-side browser environments and server-side Node.js runtimes. It serves as a polyfill and consistent interface for the `timers/promises` API introduced in Node.js, ensuring that code relying on these promise-returning timer functions works seamlessly regardless of the execution environment. Currently at stable version 1.0.1, the package is relatively new but offers a direct implementation of the Node.js specification, aiming for high compatibility. Its key differentiator is providing this modern timer API in contexts where it's not natively available, specifically browsers, or older Node.js versions, by abstracting away environmental differences. While it currently ships with version 1.0.1, its release cadence is expected to be stable after its initial minor updates.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates asynchronous timer functions (`setTimeout`, `setImmediate`, `setInterval`) that return Promises or async iterators, providing consistent timing across Node.js and browser environments.

import { setTimeout, setImmediate, setInterval } from 'isomorphic-timers-promises';

(async () => {
	console.log('--- setTimeout ---');
	const result = await setTimeout(100, 'becky');
	console.log(result); // 'becky'
})();

(async () => {
	console.log('--- setImmediate ---');
	const result = await setImmediate('maya');
	console.log(result); // 'maya'
})();

(async () => {
	console.log('--- setInterval (async iterator) ---');
	let result = 0;
	for await (const startTime of setInterval(100, Date.now())) {
		const now = Date.now();
		result = result + 1;
		if (now - startTime >= 300) { // Limit to 3 intervals for quick demo
			break;
		}
	}
	console.log(`Intervals completed: ${result}`); // Should be around 3
})();

view raw JSON →