delay
- Version 6.0.0
- Published
- 10.3 kB
- No dependencies
- MIT license
Install
npm i delay
yarn add delay
pnpm add delay
Overview
Delay a promise a specified amount of time
Index
Functions
Type Aliases
Functions
function clearDelay
clearDelay: (delayPromise: Promise<unknown>) => void;
Clears the delay and settles the promise.
If you pass in a promise that is already cleared or a promise coming from somewhere else, it does nothing.
Example 1
import delay, {clearDelay} from 'delay';const delayedPromise = delay(1000, {value: 'Done'});setTimeout(() => {clearDelay(delayedPromise);}, 500);// 500 milliseconds laterconsole.log(await delayedPromise);//=> 'Done'
function createDelay
createDelay: (timers: { clearTimeout: (timeoutId: any) => void; setTimeout: ( callback: (...args: any[]) => void, milliseconds: number, ...args: any[] ) => unknown;}) => typeof delay;
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 usedelay
to manage your tests.Example 1
import {createDelay} from 'delay';const customDelay = createDelay({clearTimeout, setTimeout});const result = await customDelay(100, {value: '🦄'});// Executed after 100 millisecondsconsole.log(result);//=> '🦄'
function delay
delay: <T>(milliseconds: number, options?: Options<T>) => Promise<T>;
Create a promise which resolves after the specified
milliseconds
.Parameter milliseconds
Milliseconds to delay the promise.
Returns
A promise which resolves after the specified
milliseconds
.Example 1
import delay from 'delay';bar();await delay(100);// Executed 100 milliseconds laterbaz();
function rangeDelay
rangeDelay: <T>( minimum: number, maximum: number, options?: Options<T>) => Promise<T>;
Create a promise which resolves after a random amount of milliseconds between
minimum
andmaximum
has passed.Useful for tests and web scraping since they can have unpredictable performance. For example, if you have a test that asserts a method should not take longer than a certain amount of time, and then run it on a CI, it could take longer. So with this method, you could give it a threshold instead.
Parameter minimum
Minimum amount of milliseconds to delay the promise.
Parameter maximum
Maximum amount of milliseconds to delay the promise.
Returns
A promise which resolves after a random amount of milliseconds between
maximum
andmaximum
has passed.
Type Aliases
type Options
type Options<T> = { /** A value to resolve in the returned promise.
@example ``` import delay from 'delay';
const result = await delay(100, {value: '🦄'});
// Executed after 100 milliseconds console.log(result); //=> '🦄' ``` */ value?: T;
/** An `AbortSignal` to abort the delay.
The returned promise will be rejected with an `AbortError` if the signal is aborted.
@example ``` import delay from 'delay';
const abortController = new AbortController();
setTimeout(() => { abortController.abort(); }, 500);
try { await delay(1000, {signal: abortController.signal}); } catch (error) { // 500 milliseconds later console.log(error.name) //=> 'AbortError' } ``` */ signal?: AbortSignal;};
Package Files (1)
Dependencies (0)
No dependencies.
Dev Dependencies (5)
Peer Dependencies (0)
No peer dependencies.
Badge
To add a badge like this oneto your package's README, use the codes available below.
You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/delay
.
- Markdown[![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/delay)
- HTML<a href="https://www.jsdocs.io/package/delay"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>
- Updated .
Package analyzed in 1594 ms. - Missing or incorrect documentation? Open an issue for this package.