mirror of
https://github.com/toss/es-toolkit.git
synced 2024-11-28 03:34:26 +03:00
8d80869fef
* feat: Support AbortSignal to delay for improved cancellation * docs: add AbortSignal in delay * refactor: add once setting in addEventListener * fix: abortError sentence * feat: separate error file
1.2 KiB
1.2 KiB
delay
Delays the execution of code for a specified number of milliseconds.
This function returns a Promise that resolves after the specified delay, allowing you to use it with async/await to pause execution. It also supports an optional AbortSignal to cancel the delay.
Signature
function delay(ms: number, options?: DelayOptions): Promise<void>;
Parameters
ms
(number
): The number of milliseconds to delay.options
(DelayOptions
, optional): An options object.signal
(AbortSignal
, optional): An optionalAbortSignal
to cancel the delay.
Returns
(Promise<void>
): A Promise that resolves after the specified delay.
Examples
Basic Usage
async function foo() {
console.log('Start');
await delay(1000); // Delays execution for 1 second
console.log('End');
}
foo();
Using with an AbortSignal
async function foo() {
const controller = new AbortController();
const signal = controller.signal;
setTimeout(() => controller.abort(), 50); // Will cancel the delay after 50ms
try {
await delay(1000, { signal });
} catch (error) {
console.log(error); // Will log 'The operation was aborted'
}
}