Comparing version 6.0.0 to 6.1.0
@@ -46,7 +46,19 @@ export class TimeoutError extends Error { | ||
/** | ||
Specify a custom error message or error. | ||
Specify a custom error message or error to throw when it times out: | ||
If you do a custom error, it's recommended to sub-class `pTimeout.TimeoutError`. | ||
- `message: 'too slow'` will throw `TimeoutError('too slow')` | ||
- `message: new MyCustomError('it’s over 9000')` will throw the same error instance | ||
- `message: false` will make the promise resolve with `undefined` instead of rejecting | ||
If you do a custom error, it's recommended to sub-class `TimeoutError`: | ||
``` | ||
import {TimeoutError} from 'p-timeout'; | ||
class MyCustomError extends TimeoutError { | ||
name = "MyCustomError"; | ||
} | ||
``` | ||
*/ | ||
message?: string | Error; | ||
message?: string | Error | false; | ||
@@ -135,3 +147,7 @@ /** | ||
input: PromiseLike<ValueType>, | ||
options: Options<ReturnType> & {message: false} | ||
): ClearablePromise<ValueType | ReturnType | undefined>; | ||
export default function pTimeout<ValueType, ReturnType = ValueType>( | ||
input: PromiseLike<ValueType>, | ||
options: Options<ReturnType> | ||
): ClearablePromise<ValueType | ReturnType>; |
12
index.js
@@ -80,5 +80,2 @@ export class TimeoutError extends Error { | ||
const errorMessage = typeof message === 'string' ? message : `Promise timed out after ${milliseconds} milliseconds`; | ||
const timeoutError = message instanceof Error ? message : new TimeoutError(errorMessage); | ||
if (typeof promise.cancel === 'function') { | ||
@@ -88,3 +85,10 @@ promise.cancel(); | ||
reject(timeoutError); | ||
if (message === false) { | ||
resolve(); | ||
} else if (message instanceof Error) { | ||
reject(message); | ||
} else { | ||
const errorMessage = message ?? `Promise timed out after ${milliseconds} milliseconds`; | ||
reject(new TimeoutError(errorMessage)); | ||
} | ||
}, milliseconds); | ||
@@ -91,0 +95,0 @@ |
{ | ||
"name": "p-timeout", | ||
"version": "6.0.0", | ||
"version": "6.1.0", | ||
"description": "Timeout a promise after a specified amount of time", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -53,9 +53,21 @@ # p-timeout | ||
Type: `string | Error`\ | ||
Type: `string | Error | false`\ | ||
Default: `'Promise timed out after 50 milliseconds'` | ||
Specify a custom error message or error. | ||
Specify a custom error message or error to throw when it times out: | ||
If you do a custom error, it's recommended to sub-class `pTimeout.TimeoutError`. | ||
- `message: 'too slow'` will throw `TimeoutError('too slow')` | ||
- `message: new MyCustomError('it’s over 9000')` will throw the same error instance | ||
- `message: false` will make the promise resolve with `undefined` instead of rejecting | ||
If you do a custom error, it's recommended to sub-class `TimeoutError`: | ||
```js | ||
import {TimeoutError} from 'p-timeout'; | ||
class MyCustomError extends TimeoutError { | ||
name = "MyCustomError"; | ||
} | ||
``` | ||
##### fallback | ||
@@ -62,0 +74,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
11838
213
159