|
Last change
on this file was a762898, checked in by istevanoska <ilinastevanoska@…>, 5 months ago |
|
Added visualizations
|
-
Property mode
set to
100644
|
|
File size:
1.1 KB
|
| Line | |
|---|
| 1 | interface DelayOptions {
|
|---|
| 2 | signal?: AbortSignal;
|
|---|
| 3 | }
|
|---|
| 4 | /**
|
|---|
| 5 | * Delays the execution of code for a specified number of milliseconds.
|
|---|
| 6 | *
|
|---|
| 7 | * This function returns a Promise that resolves after the specified delay, allowing you to use it
|
|---|
| 8 | * with async/await to pause execution.
|
|---|
| 9 | *
|
|---|
| 10 | * @param {number} ms - The number of milliseconds to delay.
|
|---|
| 11 | * @param {DelayOptions} options - The options object.
|
|---|
| 12 | * @param {AbortSignal} options.signal - An optional AbortSignal to cancel the delay.
|
|---|
| 13 | * @returns {Promise<void>} A Promise that resolves after the specified delay.
|
|---|
| 14 | *
|
|---|
| 15 | * @example
|
|---|
| 16 | * async function foo() {
|
|---|
| 17 | * console.log('Start');
|
|---|
| 18 | * await delay(1000); // Delays execution for 1 second
|
|---|
| 19 | * console.log('End');
|
|---|
| 20 | * }
|
|---|
| 21 | *
|
|---|
| 22 | * foo();
|
|---|
| 23 | *
|
|---|
| 24 | * // With AbortSignal
|
|---|
| 25 | * const controller = new AbortController();
|
|---|
| 26 | * const { signal } = controller;
|
|---|
| 27 | *
|
|---|
| 28 | * setTimeout(() => controller.abort(), 50); // Will cancel the delay after 50ms
|
|---|
| 29 | * try {
|
|---|
| 30 | * await delay(100, { signal });
|
|---|
| 31 | * } catch (error) {
|
|---|
| 32 | * console.error(error); // Will log 'AbortError'
|
|---|
| 33 | * }
|
|---|
| 34 | * }
|
|---|
| 35 | */
|
|---|
| 36 | declare function delay(ms: number, { signal }?: DelayOptions): Promise<void>;
|
|---|
| 37 |
|
|---|
| 38 | export { delay };
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.