delay

WebJar for delay

License

License

MIT
GroupId

GroupId

org.webjars.npm
ArtifactId

ArtifactId

delay
Last Version

Last Version

4.1.0
Release Date

Release Date

Type

Type

jar
Description

Description

delay
WebJar for delay
Project URL

Project URL

http://webjars.org
Source Code Management

Source Code Management

https://github.com/sindresorhus/delay

Download delay

How to add to project

<!-- https://jarcasting.com/artifacts/org.webjars.npm/delay/ -->
<dependency>
    <groupId>org.webjars.npm</groupId>
    <artifactId>delay</artifactId>
    <version>4.1.0</version>
</dependency>
// https://jarcasting.com/artifacts/org.webjars.npm/delay/
implementation 'org.webjars.npm:delay:4.1.0'
// https://jarcasting.com/artifacts/org.webjars.npm/delay/
implementation ("org.webjars.npm:delay:4.1.0")
'org.webjars.npm:delay:jar:4.1.0'
<dependency org="org.webjars.npm" name="delay" rev="4.1.0">
  <artifact name="delay" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.webjars.npm', module='delay', version='4.1.0')
)
libraryDependencies += "org.webjars.npm" % "delay" % "4.1.0"
[org.webjars.npm/delay "4.1.0"]

Dependencies

There are no dependencies for this project. It is a standalone project that does not depend on any other jars.

Project Modules

There are no modules declared in this project.

delay

Delay a promise a specified amount of time

If you target Node.js 15 or later, you can do await require('timers/promises').setTimeout(1000) instead.

Install

$ npm install delay

Usage

const delay = require('delay');

(async () => {
	bar();

	await delay(100);

	// Executed 100 milliseconds later
	baz();
})();

API

delay(milliseconds, options?)

Create a promise which resolves after the specified milliseconds.

delay.reject(milliseconds, options?)

Create a promise which rejects after the specified milliseconds.

delay.range(minimum, maximum, options?)

Create a promise which resolves after a random amount of milliseconds between minimum and maximum 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 .range(), you could give it a threshold instead.

milliseconds

mininum

maximum

Type: number

Milliseconds to delay the promise.

options

Type: object

value

Type: unknown

Optional value to resolve or reject in the returned promise.

signal

Type: AbortSignal

The returned promise will be rejected with an AbortError if the signal is aborted. AbortSignal is available in all modern browsers and there is a ponyfill for Node.js.

delayPromise.clear()

Clears the delay and settles the promise.

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

Passing a value:

const delay = require('delay');

(async() => {
	const result = await delay(100, {value: 'πŸ¦„'});

	// Executed after 100 milliseconds
	console.log(result);
	//=> 'πŸ¦„'
})();

Using delay.reject(), which optionally accepts a value and rejects it ms later:

const delay = require('delay');

(async () => {
	try {
		await delay.reject(100, {value: new Error('πŸ¦„')});

		console.log('This is never executed');
	} catch (error) {
		// 100 milliseconds later
		console.log(error);
		//=> [Error: πŸ¦„]
	}
})();

You can settle the delay early by calling .clear():

const delay = require('delay');

(async () => {
	const delayedPromise = delay(1000, {value: 'Done'});

	setTimeout(() => {
		delayedPromise.clear();
	}, 500);

	// 500 milliseconds later
	console.log(await delayedPromise);
	//=> 'Done'
})();

You can abort the delay with an AbortSignal:

const delay = require('delay');

(async () => {
	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'
	}
})();

Create a new instance that is unaffected by libraries such as lolex:

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

Versions

Version
4.1.0