2019-03-12 08:17:11 +00:00
|
|
|
/**
|
|
|
|
* Timeout a promise after a specified amount of time.
|
|
|
|
*
|
|
|
|
* If you pass in a cancelable promise, specifically a promise with a `.cancel()` method, that method will be called when the `pTimeout` promise times out.
|
|
|
|
*
|
|
|
|
* @param input - Promise to decorate.
|
2019-03-12 08:21:47 +00:00
|
|
|
* @param milliseconds - Milliseconds before timing out.
|
2019-03-12 08:17:11 +00:00
|
|
|
* @param message - Specify a custom error message or error. If you do a custom error, it's recommended to sub-class `pTimeout.TimeoutError`. Default: `'Promise timed out after 50 milliseconds'`.
|
2019-03-12 08:21:47 +00:00
|
|
|
* @returns A decorated `input` that times out after `milliseconds` time.
|
2019-03-12 08:17:11 +00:00
|
|
|
*/
|
|
|
|
export default function pTimeout<ValueType>(
|
|
|
|
input: PromiseLike<ValueType>,
|
2019-03-12 08:21:47 +00:00
|
|
|
milliseconds: number,
|
2019-03-12 08:17:11 +00:00
|
|
|
message?: string | Error
|
|
|
|
): Promise<ValueType>;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Timeout a promise after a specified amount of time.
|
|
|
|
*
|
|
|
|
* If you pass in a cancelable promise, specifically a promise with a `.cancel()` method, that method will be called when the `pTimeout` promise times out.
|
|
|
|
*
|
|
|
|
* @param input - Promise to decorate.
|
2019-03-12 08:21:47 +00:00
|
|
|
* @param milliseconds - Milliseconds before timing out.
|
2019-03-12 08:17:11 +00:00
|
|
|
* @param fallback - Do something other than rejecting with an error on timeout. You could for example retry.
|
2019-03-12 08:21:47 +00:00
|
|
|
* @returns A decorated `input` that times out after `milliseconds` time.
|
2019-03-12 08:17:11 +00:00
|
|
|
*
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* import delay from 'delay';
|
|
|
|
* import pTimeout from 'p-timeout';
|
|
|
|
*
|
|
|
|
* const delayedPromise = () => delay(200);
|
|
|
|
*
|
|
|
|
* pTimeout(delayedPromise(), 50, () => {
|
|
|
|
* return pTimeout(delayedPromise(), 300);
|
|
|
|
* });
|
|
|
|
*/
|
|
|
|
export default function pTimeout<ValueType, ReturnType>(
|
|
|
|
input: PromiseLike<ValueType>,
|
2019-03-12 08:21:47 +00:00
|
|
|
milliseconds: number,
|
2019-03-12 08:17:11 +00:00
|
|
|
fallback: () => ReturnType | Promise<ReturnType>
|
|
|
|
): Promise<ValueType | ReturnType>;
|
|
|
|
|
|
|
|
export class TimeoutError extends Error {
|
|
|
|
readonly name: 'TimeoutError';
|
|
|
|
constructor(message?: string);
|
|
|
|
}
|