Comparing version 3.2.0 to 4.0.0
@@ -8,5 +8,42 @@ declare class TimeoutErrorClass extends Error { | ||
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; | ||
}; | ||
}; | ||
} | ||
declare const pTimeout: { | ||
TimeoutError: typeof TimeoutErrorClass; | ||
default: typeof pTimeout; | ||
/** | ||
@@ -36,3 +73,4 @@ Timeout a promise after a specified amount of time. | ||
milliseconds: number, | ||
message?: string | Error | ||
message?: string | Error, | ||
options?: pTimeout.Options | ||
): Promise<ValueType>; | ||
@@ -65,11 +103,7 @@ | ||
milliseconds: number, | ||
fallback: () => ReturnType | Promise<ReturnType> | ||
fallback: () => ReturnType | Promise<ReturnType>, | ||
options?: pTimeout.Options | ||
): Promise<ValueType | ReturnType>; | ||
TimeoutError: typeof TimeoutErrorClass; | ||
// TODO: Remove this for the next major release | ||
default: typeof pTimeout; | ||
}; | ||
export = pTimeout; |
26
index.js
'use strict'; | ||
const pFinally = require('p-finally'); | ||
class TimeoutError extends Error { | ||
@@ -12,3 +10,3 @@ constructor(message) { | ||
const pTimeout = (promise, milliseconds, fallback) => new Promise((resolve, reject) => { | ||
const pTimeout = (promise, milliseconds, fallback, options) => new Promise((resolve, reject) => { | ||
if (typeof milliseconds !== 'number' || milliseconds < 0) { | ||
@@ -23,3 +21,8 @@ throw new TypeError('Expected `milliseconds` to be a positive number'); | ||
const timer = setTimeout(() => { | ||
options = { | ||
customTimers: {setTimeout, clearTimeout}, | ||
...options | ||
}; | ||
const timer = options.customTimers.setTimeout(() => { | ||
if (typeof fallback === 'function') { | ||
@@ -45,10 +48,11 @@ try { | ||
// TODO: Use native `finally` keyword when targeting Node.js 10 | ||
pFinally( | ||
// eslint-disable-next-line promise/prefer-await-to-then | ||
promise.then(resolve, reject), | ||
() => { | ||
clearTimeout(timer); | ||
(async () => { | ||
try { | ||
resolve(await promise); | ||
} catch (error) { | ||
reject(error); | ||
} finally { | ||
options.customTimers.clearTimeout(timer); | ||
} | ||
); | ||
})(); | ||
}); | ||
@@ -55,0 +59,0 @@ |
{ | ||
"name": "p-timeout", | ||
"version": "3.2.0", | ||
"version": "4.0.0", | ||
"description": "Timeout a promise after a specified amount of time", | ||
@@ -10,6 +10,6 @@ "license": "MIT", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
"url": "https://sindresorhus.com" | ||
}, | ||
"engines": { | ||
"node": ">=8" | ||
"node": ">=10" | ||
}, | ||
@@ -36,12 +36,9 @@ "scripts": { | ||
], | ||
"dependencies": { | ||
"p-finally": "^1.0.0" | ||
}, | ||
"devDependencies": { | ||
"ava": "^1.4.1", | ||
"delay": "^4.1.0", | ||
"ava": "^2.4.0", | ||
"delay": "^4.4.0", | ||
"p-cancelable": "^2.0.0", | ||
"tsd": "^0.7.2", | ||
"xo": "^0.24.0" | ||
"tsd": "^0.13.1", | ||
"xo": "^0.35.0" | ||
} | ||
} |
@@ -1,6 +0,5 @@ | ||
# p-timeout [![Build Status](https://travis-ci.org/sindresorhus/p-timeout.svg?branch=master)](https://travis-ci.org/sindresorhus/p-timeout) | ||
# p-timeout | ||
> Timeout a promise after a specified amount of time | ||
## Install | ||
@@ -12,3 +11,2 @@ | ||
## Usage | ||
@@ -26,7 +24,6 @@ | ||
## API | ||
### pTimeout(input, milliseconds, message?) | ||
### pTimeout(input, milliseconds, fallback?) | ||
### pTimeout(input, milliseconds, message?, options?) | ||
### pTimeout(input, milliseconds, fallback?, options?) | ||
@@ -53,3 +50,3 @@ Returns a decorated `input` that times out after `milliseconds` time. | ||
Type: `string` `Error`<br> | ||
Type: `string | Error`\ | ||
Default: `'Promise timed out after 50 milliseconds'` | ||
@@ -80,2 +77,36 @@ | ||
#### options | ||
Type: `object` | ||
##### customTimers | ||
Type: `object` with function properties `setTimeout` and `clearTimeout` | ||
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: | ||
```js | ||
const pTimeout = require('p-timeout'); | ||
const 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 | ||
} | ||
}); | ||
})(); | ||
``` | ||
### pTimeout.TimeoutError | ||
@@ -85,3 +116,2 @@ | ||
## Related | ||
@@ -88,0 +118,0 @@ |
Sorry, the diff of this file is not supported yet
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
8880
0
133
118
- Removedp-finally@^1.0.0
- Removedp-finally@1.0.0(transitive)