Socket
Socket
Sign inDemoInstall

delay

Package Overview
Dependencies
0
Maintainers
2
Versions
18
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    delay

Delay a promise a specified amount of time


Version published
Maintainers
2
Install size
8.51 kB
Created

Package description

What is delay?

The 'delay' npm package is a simple utility that allows you to pause the execution of an asynchronous function for a specified amount of time. It is primarily used to introduce delays in promise chains or async functions, making it useful for testing, rate limiting, or creating time-based behavior in applications.

What are delay's main functionalities?

Basic Delay

This feature allows you to pause the execution of code within an async function for a specified duration (in milliseconds). In this example, the code waits for 2 seconds before printing '2 seconds later'.

const delay = require('delay');

(async () => {
  console.log('Waiting...');
  await delay(2000);
  console.log('2 seconds later');
})();

Delay with Value

This feature enables you to resolve a promise with a specific value after a delay. Here, the promise resolves with the string 'Hello after 1.5 seconds' after waiting for 1.5 seconds.

const delay = require('delay');

(async () => {
  const result = await delay(1500, {value: 'Hello after 1.5 seconds'});
  console.log(result);
})();

Delay with Options

This feature supports passing an options object that can include an AbortSignal to cancel the delay. If the abort signal is triggered, the delay is cancelled, and the subsequent code may not execute.

const delay = require('delay');

(async () => {
  await delay(1000, {signal: someAbortSignal});
  console.log('This will not run if the abort signal is triggered');
})();

Other packages similar to delay

Readme

Source

delay Build Status

Delay a promise a specified amount of time

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.

milliseconds

Type: number

Milliseconds to delay the promise.

options

Type: Object

value

Type: any

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.

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'
	}
})();
  • delay-cli - CLI for this module
  • p-cancelable - Create a promise that can be canceled
  • p-min-delay - Delay a promise a minimum amount of time
  • p-immediate - Returns a promise resolved in the next event loop - think setImmediate()
  • p-timeout - Timeout a promise after a specified amount of time
  • More…

License

MIT © Sindre Sorhus

Keywords

FAQs

Last updated on 03 Sep 2018

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc