Isomorphic Timers Promises
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
-
Module not found: Error: Can't resolve 'timers/promises'
cause A module bundler (like Webpack or Rollup) is attempting to resolve the Node.js built-in module 'timers/promises' in a browser environment without an alias.fixAdd an alias in your bundler configuration (e.g., `webpack.config.js` or `rollup.config.js`) to map `'timers/promises'` to `'isomorphic-timers-promises'`. -
TypeError: (0 , _isomorphicTimersPromises.setTimeout) is not a function
cause This error often occurs when attempting to import named exports (like `setTimeout`) as a default export, or using CommonJS `require` syntax in an ESM-only context.fixEnsure you are using named imports: `import { setTimeout, setImmediate, setInterval } from 'isomorphic-timers-promises';`
Warnings
- gotcha Module bundlers (Webpack, Rollup) require explicit configuration to correctly alias `timers/promises` to `isomorphic-timers-promises` when targeting browser environments, as `timers/promises` is a Node.js built-in module.
- gotcha The `options.ref` parameter, used to control whether a timer requires the event loop to remain active, is only effective in Node.js server environments and will be ignored in browser contexts.
- gotcha Unlike `setTimeout` and `setImmediate` which return Promises, `setInterval` returns an async iterator. It must be consumed with `for await...of` to receive emitted values.
Install
-
npm install isomorphic-timers-promises -
yarn add isomorphic-timers-promises -
pnpm add isomorphic-timers-promises
Imports
- setTimeout
const { setTimeout } = require('isomorphic-timers-promises');import { setTimeout } from 'isomorphic-timers-promises'; - setImmediate
import setImmediate from 'isomorphic-timers-promises';
import { setImmediate } from 'isomorphic-timers-promises'; - setInterval
import { setInterval as browserSetInterval } from 'isomorphic-timers-promises/browser';import { setInterval } from 'isomorphic-timers-promises';
Quickstart
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
})();