Security News
Cloudflare Adds Security.txt Setup Wizard
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
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.
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');
})();
Similar to 'delay', 'timeout' is used to introduce a delay in asynchronous operations. However, it focuses more on setting timeouts for promises, potentially rejecting them if they take too long, which is a slight functional shift from simply delaying.
This package offers functionality similar to 'delay' by resolving a promise after a specified timeout. The main difference is in the API design and naming conventions, but the core functionality of introducing delays in promise-based workflows is very similar.
While 'p-timeout' provides delay functionalities, it is primarily designed to add timeout capabilities to promises. It can reject a promise if it does not settle within a specified period, which is a feature not provided by '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.
$ npm install delay
const delay = require('delay');
(async () => {
bar();
await delay(100);
// Executed 100 milliseconds later
baz();
})();
Create a promise which resolves after the specified milliseconds
.
Create a promise which rejects after the specified milliseconds
.
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.
Type: number
Milliseconds to delay the promise.
Type: object
Type: unknown
Optional value to resolve or reject in the returned promise.
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.
Clears the delay and settles the promise.
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.
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);
//=> '🦄'
})();
setImmediate()
FAQs
Delay a promise a specified amount of time
The npm package delay receives a total of 3,036,591 weekly downloads. As such, delay popularity was classified as popular.
We found that delay demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
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.
Security News
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
Security News
The Socket Research team breaks down a malicious npm package targeting the legitimate DOMPurify library. It uses obfuscated code to hide that it is exfiltrating browser and crypto wallet data.
Security News
ENISA’s 2024 report highlights the EU’s top cybersecurity threats, including rising DDoS attacks, ransomware, supply chain vulnerabilities, and weaponized AI.