Comparing version 5.0.2 to 5.1.0
@@ -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 @@ |
42
index.js
@@ -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') { |
{ | ||
"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 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
11328
186
141