Comparing version 4.0.1 to 4.1.0
@@ -1,4 +0,2 @@ | ||
/// <reference lib="dom"/> | ||
interface ClearablePromise<T> extends Promise<T> { | ||
export interface ClearablePromise<T> extends Promise<T> { | ||
/** | ||
@@ -10,3 +8,14 @@ * Clears the delay and settles the promise. | ||
interface DelayOptions { | ||
/** | ||
* Minimal subset of `AbortSignal` that delay will use if passed. | ||
* This avoids a dependency on dom.d.ts. | ||
* The dom.d.ts `AbortSignal` is compatible with this one. | ||
*/ | ||
interface AbortSignal { | ||
readonly aborted: boolean; | ||
addEventListener(type: 'abort', listener: () => void, options?: { once?: boolean }): void; | ||
removeEventListener(type: 'abort', listener: () => void): void; | ||
} | ||
export interface Options { | ||
/** | ||
@@ -19,3 +28,3 @@ * An optional AbortSignal to abort the delay. | ||
declare const delay: { | ||
type Delay = { | ||
/** | ||
@@ -27,3 +36,3 @@ * Create a promise which resolves after the specified `milliseconds`. | ||
*/ | ||
(milliseconds: number, options?: DelayOptions): ClearablePromise<void>; | ||
(milliseconds: number, options?: Options): ClearablePromise<void>; | ||
@@ -36,3 +45,3 @@ /** | ||
*/ | ||
<T>(milliseconds: number, options?: DelayOptions & { | ||
<T>(milliseconds: number, options?: Options & { | ||
/** Value to resolve in the returned promise. */ | ||
@@ -49,8 +58,12 @@ value: T | ||
// TODO: Allow providing value type after https://github.com/Microsoft/TypeScript/issues/5413 will be resolved. | ||
reject(milliseconds: number, options?: DelayOptions & { | ||
reject(milliseconds: number, options?: Options & { | ||
/** Value to reject in the returned promise. */ | ||
value?: any | ||
}): ClearablePromise<never>; | ||
} | ||
declare const delay: Delay & { | ||
createWithTimers(timers: {clearTimeout: typeof clearTimeout, setTimeout: typeof setTimeout}): Delay | ||
}; | ||
export default delay; |
17
index.js
@@ -9,3 +9,3 @@ 'use strict'; | ||
const createDelay = willResolve => (ms, {value, signal} = {}) => { | ||
const createDelay = ({clearTimeout: clear = clearTimeout, setTimeout: set = setTimeout, willResolve}) => (ms, {value, signal} = {}) => { | ||
if (signal && signal.aborted) { | ||
@@ -20,3 +20,3 @@ return Promise.reject(createAbortError()); | ||
const signalListener = () => { | ||
clearTimeout(timeoutId); | ||
clear(timeoutId); | ||
rejectFn(createAbortError()); | ||
@@ -41,3 +41,3 @@ }; | ||
rejectFn = reject; | ||
timeoutId = setTimeout(settle, ms); | ||
timeoutId = set(settle, ms); | ||
}); | ||
@@ -50,3 +50,3 @@ | ||
delayPromise.clear = () => { | ||
clearTimeout(timeoutId); | ||
clear(timeoutId); | ||
timeoutId = null; | ||
@@ -60,5 +60,10 @@ cleanup(); | ||
const delay = createDelay(true); | ||
delay.reject = createDelay(false); | ||
const delay = createDelay({willResolve: true}); | ||
delay.reject = createDelay({willResolve: false}); | ||
delay.createWithTimers = ({clearTimeout, setTimeout}) => { | ||
const delay = createDelay({clearTimeout, setTimeout, willResolve: true}); | ||
delay.reject = createDelay({clearTimeout, setTimeout, willResolve: false}); | ||
return delay; | ||
}; | ||
module.exports = delay; | ||
module.exports.default = delay; |
{ | ||
"name": "delay", | ||
"version": "4.0.1", | ||
"version": "4.1.0", | ||
"description": "Delay a promise a specified amount of time", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -65,3 +65,6 @@ # delay [![Build Status](https://travis-ci.org/sindresorhus/delay.svg?branch=master)](https://travis-ci.org/sindresorhus/delay) | ||
### delay.createWithTimers({clearTimeout, setTimeout}) | ||
Creates a new `delay` instance using the provided functions for clearing and setting timeouts. Useful if you're about to stub timers globally, but you still want to use `delay` to manage your tests. | ||
## Advanced usage | ||
@@ -141,3 +144,19 @@ | ||
Create a new instance that is unaffected by libraries such as [lolex](https://github.com/sinonjs/lolex/): | ||
```js | ||
const delay = require('delay'); | ||
const customDelay = delay.createWithTimers({clearTimeout, setTimeout}); | ||
(async() => { | ||
const result = await customDelay(100, {value: '🦄'}); | ||
// Executed after 100 milliseconds | ||
console.log(result); | ||
//=> '🦄' | ||
})(); | ||
``` | ||
## Related | ||
@@ -144,0 +163,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
9069
111
173