![require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages](https://cdn.sanity.io/images/cgdhsj6q/production/be8ab80c8efa5907bc341c6fefe9aa20d239d890-1600x1097.png?w=400&fit=max&auto=format)
Security News
require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
isomorphic-timers-promises
Advanced tools
The isomorphic-timers-promises npm package provides a set of timer functions that return promises, making it easier to work with asynchronous code. It is designed to work in both Node.js and browser environments, hence the term 'isomorphic'.
setTimeout
The setTimeout function returns a promise that resolves after a specified number of milliseconds. This is useful for delaying the execution of code.
const { setTimeout } = require('isomorphic-timers-promises');
setTimeout(1000).then(() => {
console.log('1 second has passed');
});
setInterval
The setInterval function returns an interval ID and executes a callback function at specified intervals. The clearInterval function can be used to stop the interval.
const { setInterval, clearInterval } = require('isomorphic-timers-promises');
const intervalId = setInterval(1000, () => {
console.log('1 second interval');
});
// To clear the interval after 5 seconds
setTimeout(5000).then(() => clearInterval(intervalId));
setImmediate
The setImmediate function returns a promise that resolves immediately after the current event loop, allowing you to execute code asynchronously without any delay.
const { setImmediate } = require('isomorphic-timers-promises');
setImmediate().then(() => {
console.log('This runs immediately after the current event loop');
});
The timers-promises package from Node.js provides similar functionality, offering promise-based versions of setTimeout, setInterval, and setImmediate. It is more tightly integrated with Node.js but lacks isomorphic support for browser environments.
The delay package is a simple utility for delaying execution using promises. It primarily focuses on the setTimeout functionality and is widely used for creating delays in asynchronous code. However, it does not offer setInterval or setImmediate functionalities.
The sleep-promise package provides a promise-based sleep function that pauses execution for a specified duration. It is similar to the setTimeout functionality in isomorphic-timers-promises but does not include setInterval or setImmediate.
timers/promises
for client and server.
The
timers/promises
API provides an alternative set of timer functions that returnPromise
objects.
npm install isomorphic-timers-promises --save
import {
setTimeout,
setImmediate,
setInterval
} from 'isomorphic-timers-promises';
(async () => {
const result = await setTimeout(100, 'becky');
console.log(result); // 'becky'
})();
(async () => {
const result = await setImmediate('maya');
console.log(result); // 'maya'
})();
(async () => {
let result = 0;
for await (const startTime of setInterval(100, Date.now())) {
const now = Date.now();
result = result + 1;
if (now - startTime >= 1000) {
break;
}
}
console.log(result); // 10
})();
// webpack.config.js
module.exports = {
// ...
resolve: {
alias: {
'timers/promises': 'isomorphic-timers-promises'
}
}
};
// rollup.config.js
const { default: resolve } = require('@rollup/plugin-node-resolve');
const alias = require('@rollup/plugin-alias');
module.exports = {
// ...
plugins: [
resolve(),
alias({
entries: {
'timers/promises': 'isomorphic-timers-promises'
}
})
]
};
Returns: Promise
Property | Type | Default | Description |
---|---|---|---|
delay | number | 1 | The number of milliseconds to wait before fulfilling the promise. |
value | * | A value with which the promise is fulfilled. | |
options.ref | boolean | true | Set to false to indicate that the scheduled timeout should not require the event loop to remain active. Valid only for server environment. |
options.signal | AbortSignal | An optional AbortSignal that can be used to cancel the scheduled timeout. |
Returns: Promise
Property | Type | Default | Description |
---|---|---|---|
value | * | A value with which the promise is fulfilled. | |
options.ref | boolean | true | Set to false to indicate that the scheduled immediate should not require the event loop to remain active. Valid only for server environment. |
options.signal | AbortSignal | An optional AbortSignal that can be used to cancel the scheduled immediate. |
Returns: async iterator that generates values in an interval of delay
.
Property | Type | Default | Description |
---|---|---|---|
delay | number | 1 | The number of milliseconds to wait between iterations. |
value | * | A value with which the iterator returns. | |
options.ref | boolean | true | Set to false to indicate that the scheduled timeout between iterations should not require the event loop to remain active. Valid only for server environment. |
options.signal | AbortSignal | An optional AbortSignal that can be used to cancel the scheduled timeout between operations. |
Supports Node 10+.
Tested in Chrome 72, Firefox 65, Internet Explorer 11 and should work in all modern browsers.
Check support based on Browserslist configuration).
Assumes Promise
, AbortController
and setImmediate
are polyfilled or
available in global context.
For automated tests, run npm run test:automated
(append :watch
for watcher
support).
Test suite is taken and modified from official Node.js repository
(setTimeout
,
setImmediate
,
setInterval
).
MIT © Ivan Nikolić
[1.0.1][] - 2021-06-30
FAQs
`timers/promises` for client and server.
The npm package isomorphic-timers-promises receives a total of 386,221 weekly downloads. As such, isomorphic-timers-promises popularity was classified as popular.
We found that isomorphic-timers-promises demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.