Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

p-timeout

Package Overview
Dependencies
Maintainers
1
Versions
22
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 5.0.2 to 5.1.0

25

index.d.ts

@@ -44,2 +44,27 @@ /* eslint-disable import/export */

};
/**
You can abort the promise using [`AbortController`](https://developer.mozilla.org/en-US/docs/Web/API/AbortController).
_Requires Node.js 16 or later._
@example
```
import pTimeout from 'p-timeout';
import delay from 'delay';
const delayedPromise = delay(3000);
const abortController = new AbortController();
setTimeout(() => {
abortController.abort();
}, 100);
await pTimeout(delayedPromise, 2000, undefined, {
signal: abortController.signal
});
```
*/
signal?: globalThis.AbortSignal;
};

@@ -46,0 +71,0 @@

@@ -8,4 +8,35 @@ export class TimeoutError extends Error {

/**
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;
}
}
/**
TODO: Remove AbortError and just throw DOMException when targeting Node 18.
*/
const getDOMException = errorMessage => globalThis.DOMException === undefined ?
new AbortError(errorMessage) :
new DOMException(errorMessage);
/**
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, milliseconds, fallback, options) {
let timer;
const cancelablePromise = new Promise((resolve, reject) => {

@@ -26,2 +57,13 @@ if (typeof milliseconds !== 'number' || Math.sign(milliseconds) !== 1) {

if (options.signal) {
const {signal} = options;
if (signal.aborted) {
reject(getAbortedReason(signal));
}
signal.addEventListener('abort', () => {
reject(getAbortedReason(signal));
});
}
timer = options.customTimers.setTimeout.call(undefined, () => {

@@ -28,0 +70,0 @@ if (typeof fallback === 'function') {

2

package.json
{
"name": "p-timeout",
"version": "5.0.2",
"version": "5.1.0",
"description": "Timeout a promise after a specified amount of time",

@@ -5,0 +5,0 @@ "license": "MIT",

@@ -106,2 +106,27 @@ # p-timeout

#### 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).
*Requires Node.js 16 or later.*
```js
import pTimeout from 'p-timeout';
import delay from 'delay';
const delayedPromise = delay(3000);
const abortController = new AbortController();
setTimeout(() => {
abortController.abort();
}, 100);
await pTimeout(delayedPromise, 2000, undefined, {
signal: abortController.signal
});
```
### TimeoutError

@@ -108,0 +133,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc