Refactor TypeScript definition to CommonJS compatible export (#11)
This commit is contained in:
parent
c79394a096
commit
695a09c4be
|
@ -1,47 +1,72 @@
|
|||
/**
|
||||
* 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.
|
||||
*/
|
||||
export default function pTimeout<ValueType>(
|
||||
input: PromiseLike<ValueType>,
|
||||
milliseconds: number,
|
||||
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.
|
||||
* @param milliseconds - Milliseconds before timing 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.
|
||||
*
|
||||
* @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>,
|
||||
milliseconds: number,
|
||||
fallback: () => ReturnType | Promise<ReturnType>
|
||||
): Promise<ValueType | ReturnType>;
|
||||
|
||||
export class TimeoutError extends Error {
|
||||
declare class TimeoutErrorClass extends Error {
|
||||
readonly name: 'TimeoutError';
|
||||
constructor(message?: string);
|
||||
}
|
||||
|
||||
declare namespace pTimeout {
|
||||
type TimeoutError = TimeoutErrorClass;
|
||||
}
|
||||
|
||||
declare const 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.
|
||||
|
||||
@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
|
||||
): 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.
|
||||
@param milliseconds - Milliseconds before timing 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.
|
||||
|
||||
@example
|
||||
```
|
||||
import delay = require('delay');
|
||||
import pTimeout = require('p-timeout');
|
||||
|
||||
const delayedPromise = () => delay(200);
|
||||
|
||||
pTimeout(delayedPromise(), 50, () => {
|
||||
return pTimeout(delayedPromise(), 300);
|
||||
});
|
||||
```
|
||||
*/
|
||||
<ValueType, ReturnType>(
|
||||
input: PromiseLike<ValueType>,
|
||||
milliseconds: number,
|
||||
fallback: () => ReturnType | Promise<ReturnType>
|
||||
): Promise<ValueType | ReturnType>;
|
||||
|
||||
TimeoutError: typeof TimeoutErrorClass;
|
||||
|
||||
// TODO: Remove this for the next major release
|
||||
default: typeof pTimeout;
|
||||
};
|
||||
|
||||
export = pTimeout;
|
||||
|
|
1
index.js
1
index.js
|
@ -46,6 +46,7 @@ const pTimeout = (promise, milliseconds, fallback) => new Promise((resolve, reje
|
|||
});
|
||||
|
||||
module.exports = pTimeout;
|
||||
// TODO: Remove this for the next major release
|
||||
module.exports.default = pTimeout;
|
||||
|
||||
module.exports.TimeoutError = TimeoutError;
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import {expectType} from 'tsd-check';
|
||||
import pTimeout, {TimeoutError} from '.';
|
||||
import {expectType} from 'tsd';
|
||||
import pTimeout = require('.');
|
||||
import {TimeoutError} from '.';
|
||||
|
||||
const delayedPromise: () => Promise<string> = () =>
|
||||
new Promise(resolve => setTimeout(() => resolve('foo'), 200));
|
||||
|
@ -22,4 +23,5 @@ pTimeout(delayedPromise(), 50, () => 10).then(value => {
|
|||
expectType<string | number>(value);
|
||||
});
|
||||
|
||||
expectType<typeof TimeoutError>(TimeoutError);
|
||||
const timeoutError = new TimeoutError();
|
||||
expectType<TimeoutError>(timeoutError);
|
||||
|
|
|
@ -13,7 +13,7 @@
|
|||
"node": ">=8"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "xo && ava && tsd-check"
|
||||
"test": "xo && ava && tsd"
|
||||
},
|
||||
"files": [
|
||||
"index.js",
|
||||
|
@ -36,10 +36,10 @@
|
|||
"p-finally": "^1.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"ava": "^1.3.1",
|
||||
"ava": "^1.4.1",
|
||||
"delay": "^4.1.0",
|
||||
"p-cancelable": "^1.1.0",
|
||||
"tsd-check": "^0.3.0",
|
||||
"p-cancelable": "^2.0.0",
|
||||
"tsd": "^0.7.2",
|
||||
"xo": "^0.24.0"
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue