You're Invited:Meet the Socket Team at RSAC and BSidesSF 2026, March 23–26.RSVP
Socket
Book a DemoSign in
Socket

p-timeout

Package Overview
Dependencies
Maintainers
1
Versions
25
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

p-timeout - npm Package Compare versions

Comparing version
6.1.4
to
7.0.0
+2
-4
index.d.ts
export class TimeoutError extends Error {
readonly name: 'TimeoutError';
constructor(message?: string);
constructor(message?: string, options?: ErrorOptions);
}

@@ -95,6 +95,4 @@

/**
You can abort the promise using [`AbortController`](https://developer.mozilla.org/en-US/docs/Web/API/AbortController).
Abort the promise.
_Requires Node.js 16 or later._
@example

@@ -101,0 +99,0 @@ ```

+20
-49
export class TimeoutError extends Error {
constructor(message) {
super(message);
this.name = 'TimeoutError';
}
}
name = 'TimeoutError';
/**
An error to be thrown when the request is aborted by AbortController.
DOMException is thrown instead of this Error when DOMException is available.
*/
export class AbortError extends Error {
constructor(message) {
super();
this.name = 'AbortError';
this.message = message;
constructor(message, options) {
super(message, options);
Error.captureStackTrace?.(this, TimeoutError);
}
}
/**
TODO: Remove AbortError and just throw DOMException when targeting Node 18.
*/
const getDOMException = errorMessage => globalThis.DOMException === undefined
? new AbortError(errorMessage)
: new DOMException(errorMessage);
const getAbortedReason = signal => signal.reason ?? new DOMException('This operation was aborted.', 'AbortError');
/**
TODO: Remove below function and just 'reject(signal.reason)' when targeting Node 18.
*/
const getAbortedReason = signal => {
const reason = signal.reason === undefined
? getDOMException('This operation was aborted.')
: signal.reason;
return reason instanceof Error ? reason : getDOMException(reason);
};
export default function pTimeout(promise, options) {

@@ -44,2 +18,3 @@ const {

customTimers = {setTimeout, clearTimeout},
signal,
} = options;

@@ -55,8 +30,8 @@

if (options.signal) {
const {signal} = options;
if (signal.aborted) {
reject(getAbortedReason(signal));
}
if (signal?.aborted) {
reject(getAbortedReason(signal));
return;
}
if (signal) {
abortHandler = () => {

@@ -69,4 +44,7 @@ reject(getAbortedReason(signal));

// Use .then() instead of async IIFE to preserve stack traces
// eslint-disable-next-line promise/prefer-await-to-then, promise/prefer-catch
promise.then(resolve, reject);
if (milliseconds === Number.POSITIVE_INFINITY) {
promise.then(resolve, reject);
return;

@@ -78,3 +56,3 @@ }

timer = customTimers.setTimeout.call(undefined, () => {
timer = customTimers.setTimeout(() => {
if (fallback) {

@@ -103,16 +81,9 @@ try {

}, milliseconds);
(async () => {
try {
resolve(await promise);
} catch (error) {
reject(error);
}
})();
});
// eslint-disable-next-line promise/prefer-await-to-then
const cancelablePromise = wrappedPromise.finally(() => {
cancelablePromise.clear();
if (abortHandler && options.signal) {
options.signal.removeEventListener('abort', abortHandler);
if (abortHandler && signal) {
signal.removeEventListener('abort', abortHandler);
}

@@ -122,3 +93,3 @@ });

cancelablePromise.clear = () => {
customTimers.clearTimeout.call(undefined, timer);
customTimers.clearTimeout(timer);
timer = undefined;

@@ -125,0 +96,0 @@ };

{
"name": "p-timeout",
"version": "6.1.4",
"version": "7.0.0",
"description": "Timeout a promise after a specified amount of time",

@@ -14,7 +14,9 @@ "license": "MIT",

"type": "module",
"exports": "./index.js",
"types": "./index.d.ts",
"exports": {
"types": "./index.d.ts",
"default": "./index.js"
},
"sideEffects": false,
"engines": {
"node": ">=14.16"
"node": ">=20"
},

@@ -42,11 +44,11 @@ "scripts": {

"devDependencies": {
"ava": "^4.3.1",
"delay": "^5.0.0",
"ava": "^6.4.1",
"delay": "^6.0.0",
"in-range": "^3.0.0",
"p-cancelable": "^4.0.1",
"sinon": "^19.0.2",
"sinon": "^21.0.0",
"time-span": "^5.1.0",
"tsd": "^0.22.0",
"xo": "^0.54.2"
"tsd": "^0.33.0",
"xo": "^1.2.2"
}
}

@@ -57,3 +57,3 @@ # p-timeout

Type: `string | Error | false`\
Default: `'Promise timed out after 50 milliseconds'`
Default: `'Promise timed out after {milliseconds} milliseconds'`

@@ -82,2 +82,4 @@ Specify a custom error message or error to throw when it times out:

The function can return a value or a promise.
You could for example retry:

@@ -94,4 +96,6 @@

fallback: () => {
return pTimeout(delayedPromise(), {milliseconds: 300});
},
return pTimeout(delayedPromise(), {
milliseconds: 300
});
}
});

@@ -111,4 +115,4 @@ ```

```js
import {setTimeout} from 'node:timers/promises';
import pTimeout from 'p-timeout';
import sinon from 'sinon';

@@ -130,10 +134,8 @@ const originalSetTimeout = setTimeout;

#### signal
##### signal
Type: [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal)
You can abort the promise using [`AbortController`](https://developer.mozilla.org/en-US/docs/Web/API/AbortController).
Abort the promise.
*Requires Node.js 16 or later.*
```js

@@ -172,3 +174,3 @@ import pTimeout from 'p-timeout';

Asynchronous functions like `fetch` can accept an [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal), which can be conveniently created with [`AbortSignal.timeout()`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal/timeout_static).
Async functions like `fetch` can accept an [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal), which can be conveniently created with [`AbortSignal.timeout()`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal/timeout_static).

@@ -175,0 +177,0 @@ The advantage over `p-timeout` is that the promise-generating function (like `fetch`) is actually notified that the user is no longer expecting an answer, so it can interrupt its work and free resources.