Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
promise-timeout
Advanced tools
The `promise-timeout` npm package provides utilities to add timeout functionality to promises. It allows you to set a time limit on a promise, after which it will be rejected if not resolved. This is useful for handling cases where you want to ensure that certain asynchronous operations do not take longer than a specified duration.
Timeout a Promise
This feature allows you to set a timeout on a promise. If the promise does not resolve within the specified time (1000ms in this case), it will be rejected with a timeout error.
const { timeout } = require('promise-timeout');
const myPromise = new Promise((resolve) => {
setTimeout(() => resolve('Success!'), 2000);
});
const timeoutPromise = timeout(myPromise, 1000);
timeoutPromise
.then((result) => console.log(result))
.catch((err) => console.error('Promise timed out:', err));
Timeout with Custom Error
This feature allows you to set a timeout on a promise with a custom error message. If the promise does not resolve within the specified time (1000ms in this case), it will be rejected with the custom timeout error.
const { timeout, TimeoutError } = require('promise-timeout');
const myPromise = new Promise((resolve) => {
setTimeout(() => resolve('Success!'), 2000);
});
const timeoutPromise = timeout(myPromise, 1000, new TimeoutError('Custom timeout error'));
timeoutPromise
.then((result) => console.log(result))
.catch((err) => console.error('Promise timed out:', err.message));
The `p-timeout` package provides similar functionality to `promise-timeout` by allowing you to set a timeout on a promise. It also offers additional features such as custom error messages and fallback values. Compared to `promise-timeout`, `p-timeout` has a more extensive API and better integration with other promise-based utilities.
The `promise-time` package offers timeout functionality for promises, similar to `promise-timeout`. It allows you to specify a timeout duration and will reject the promise if it does not resolve within that time. `promise-time` is simpler and more lightweight compared to `promise-timeout`, making it a good choice for projects with minimal dependencies.
A super-simple way to put a timeout on promise resolution.
It assumes you already have either platform support for promises (Node 0.12 or greater), or you have a polyfill (see es6-promise).
$ npm install promise-timeout
Rejects a promise with a TimeoutError
if it does not settle within the
specified timeout. Parameters:
promise: Promise
- Promise to monitor.timeoutMillis: number
- Number of milliseconds to wait on settling.Exception indicating that the timeout expired.
ES2015:
import { timeout, TimeoutError } from 'promise-timeout';
let somePromise = goDoSomething();
timeout(somePromise, 1000)
.then((thing) => console.log('I did a thing!'))
.catch((err) => {
if (err instanceof TimeoutError) {
console.error('Timeout :-(');
}
});
ES5:
'use strict';
var pt = require('promise-timeout');
var somePromise = goDoSomething();
pt.timeout(somePromise, 1000)
.then(function (thing) {
console.log('I did a thing!');
}).catch(function (err) {
if (err instanceof pt.TimeoutError) {
console.error('Timeout :-(');
}
});
v1.3.0 (2018-02-18)
FAQs
Simple timeouts for promises
The npm package promise-timeout receives a total of 0 weekly downloads. As such, promise-timeout popularity was classified as not popular.
We found that promise-timeout 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
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.