-
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4f86930
commit 458618e
Showing
8 changed files
with
130 additions
and
153 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,6 @@ jobs: | |
node-version: | ||
- 14 | ||
- 12 | ||
- 10 | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- uses: actions/setup-node@v1 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,113 +1,101 @@ | ||
declare class TimeoutErrorClass extends Error { | ||
/* eslint-disable import/export */ | ||
|
||
export class TimeoutError extends Error { | ||
readonly name: 'TimeoutError'; | ||
constructor(message?: string); | ||
} | ||
|
||
declare namespace pTimeout { | ||
type TimeoutError = TimeoutErrorClass; | ||
|
||
type Options = { | ||
/** | ||
Custom implementations for the `setTimeout` and `clearTimeout` functions. | ||
Useful for testing purposes, in particular to work around [`sinon.useFakeTimers()`](https://sinonjs.org/releases/latest/fake-timers/). | ||
@example | ||
``` | ||
import pTimeout = require('p-timeout'); | ||
import sinon = require('sinon'); | ||
(async () => { | ||
const originalSetTimeout = setTimeout; | ||
const originalClearTimeout = clearTimeout; | ||
sinon.useFakeTimers(); | ||
// Use `pTimeout` without being affected by `sinon.useFakeTimers()`: | ||
await pTimeout(doSomething(), 2000, undefined, { | ||
customTimers: { | ||
setTimeout: originalSetTimeout, | ||
clearTimeout: originalClearTimeout | ||
} | ||
}); | ||
})(); | ||
``` | ||
*/ | ||
readonly customTimers?: { | ||
setTimeout: typeof global.setTimeout; | ||
clearTimeout: typeof global.clearTimeout; | ||
}; | ||
}; | ||
} | ||
|
||
interface ClearablePromise<T> extends Promise<T>{ | ||
export interface ClearablePromise<T> extends Promise<T>{ | ||
/** | ||
Clear the timeout. | ||
*/ | ||
clear: () => void; | ||
} | ||
|
||
declare const pTimeout: { | ||
TimeoutError: typeof TimeoutErrorClass; | ||
|
||
default: typeof pTimeout; | ||
|
||
export type Options = { | ||
/** | ||
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. | ||
Custom implementations for the `setTimeout` and `clearTimeout` functions. | ||
@param input - Promise to decorate. | ||
@param milliseconds - Milliseconds before timing out. | ||
@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'`. | ||
@returns A decorated `input` that times out after `milliseconds` time. It has a `.clear()` method that clears the timeout. | ||
Useful for testing purposes, in particular to work around [`sinon.useFakeTimers()`](https://sinonjs.org/releases/latest/fake-timers/). | ||
@example | ||
``` | ||
import delay = require('delay'); | ||
import pTimeout = require('p-timeout'); | ||
const delayedPromise = delay(200); | ||
pTimeout(delayedPromise, 50).then(() => 'foo'); | ||
//=> [TimeoutError: Promise timed out after 50 milliseconds] | ||
``` | ||
*/ | ||
<ValueType>( | ||
input: PromiseLike<ValueType>, | ||
milliseconds: number, | ||
message?: string | Error, | ||
options?: pTimeout.Options | ||
): ClearablePromise<ValueType>; | ||
import pTimeout from 'p-timeout'; | ||
import sinon from 'sinon'; | ||
/** | ||
Timeout a promise after a specified amount of time. | ||
const originalSetTimeout = setTimeout; | ||
const originalClearTimeout = clearTimeout; | ||
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. | ||
sinon.useFakeTimers(); | ||
@param input - Promise to decorate. | ||
@param milliseconds - Milliseconds before timing out. Passing `Infinity` will cause it to never time out. | ||
@param fallback - Do something other than rejecting with an error on timeout. You could for example retry. | ||
@returns A decorated `input` that times out after `milliseconds` time. It has a `.clear()` method that clears the timeout. | ||
@example | ||
``` | ||
import delay = require('delay'); | ||
import pTimeout = require('p-timeout'); | ||
const delayedPromise = () => delay(200); | ||
pTimeout(delayedPromise(), 50, () => { | ||
return pTimeout(delayedPromise(), 300); | ||
// Use `pTimeout` without being affected by `sinon.useFakeTimers()`: | ||
await pTimeout(doSomething(), 2000, undefined, { | ||
customTimers: { | ||
setTimeout: originalSetTimeout, | ||
clearTimeout: originalClearTimeout | ||
} | ||
}); | ||
``` | ||
*/ | ||
<ValueType, ReturnType>( | ||
input: PromiseLike<ValueType>, | ||
milliseconds: number, | ||
fallback: () => ReturnType | Promise<ReturnType>, | ||
options?: pTimeout.Options | ||
): ClearablePromise<ValueType | ReturnType>; | ||
readonly customTimers?: { | ||
setTimeout: typeof global.setTimeout; | ||
clearTimeout: typeof global.clearTimeout; | ||
}; | ||
}; | ||
|
||
export = pTimeout; | ||
/** | ||
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. | ||
@param milliseconds - Milliseconds before timing out. | ||
@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'`. | ||
@returns A decorated `input` that times out after `milliseconds` time. It has a `.clear()` method that clears the timeout. | ||
@example | ||
``` | ||
import {setTimeout} from 'timers/promises'; | ||
import pTimeout from 'p-timeout'; | ||
const delayedPromise = setTimeout(200); | ||
await pTimeout(delayedPromise, 50); | ||
//=> [TimeoutError: Promise timed out after 50 milliseconds] | ||
``` | ||
*/ | ||
export default function pTimeout<ValueType>( | ||
input: PromiseLike<ValueType>, | ||
milliseconds: number, | ||
message?: string | Error, | ||
options?: Options | ||
): ClearablePromise<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. | ||
@param milliseconds - Milliseconds before timing out. Passing `Infinity` will cause it to never time out. | ||
@param fallback - Do something other than rejecting with an error on timeout. You could for example retry. | ||
@returns A decorated `input` that times out after `milliseconds` time. It has a `.clear()` method that clears the timeout. | ||
@example | ||
``` | ||
import {setTimeout} from 'timers/promises'; | ||
import pTimeout from 'p-timeout'; | ||
const delayedPromise = () => setTimeout(200); | ||
await pTimeout(delayedPromise(), 50, () => { | ||
return pTimeout(delayedPromise(), 300); | ||
}); | ||
``` | ||
*/ | ||
export default function pTimeout<ValueType, ReturnType>( | ||
input: PromiseLike<ValueType>, | ||
milliseconds: number, | ||
fallback: () => ReturnType | Promise<ReturnType>, | ||
options?: Options | ||
): ClearablePromise<ValueType | ReturnType>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,13 +4,16 @@ | |
"description": "Timeout a promise after a specified amount of time", | ||
"license": "MIT", | ||
"repository": "sindresorhus/p-timeout", | ||
"funding": "https://github.com/sponsors/sindresorhus", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "[email protected]", | ||
"url": "https://sindresorhus.com" | ||
}, | ||
"type": "module", | ||
"exports": "./index.js", | ||
"engines": { | ||
"node": ">=10" | ||
"node": ">=12" | ||
}, | ||
"scripts": { | ||
"test": "xo && ava && tsd" | ||
|
@@ -33,12 +36,12 @@ | |
"bluebird" | ||
], | ||
"devDependencies": { | ||
"ava": "^2.4.0", | ||
"delay": "^4.4.0", | ||
"p-cancelable": "^2.0.0", | ||
"tsd": "^0.13.1", | ||
"xo": "^0.35.0", | ||
"in-range": "^2.0.0", | ||
"time-span": "^4.0.0" | ||
"ava": "^3.15.0", | ||
"delay": "^5.0.0", | ||
"in-range": "^3.0.0", | ||
"p-cancelable": "^2.1.0", | ||
"time-span": "^4.0.0", | ||
"tsd": "^0.14.0", | ||
"xo": "^0.38.2" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.