|
Last change
on this file since ba17441 was a762898, checked in by istevanoska <ilinastevanoska@…>, 5 months ago |
|
Added visualizations
|
-
Property mode
set to
100644
|
|
File size:
1.0 KB
|
| Rev | Line | |
|---|
| [a762898] | 1 | /**
|
|---|
| 2 | * Executes an async function and enforces a timeout.
|
|---|
| 3 | *
|
|---|
| 4 | * If the promise does not resolve within the specified time,
|
|---|
| 5 | * the timeout will trigger and the returned promise will be rejected.
|
|---|
| 6 | *
|
|---|
| 7 | *
|
|---|
| 8 | * @template T
|
|---|
| 9 | * @param {() => Promise<T>} run - A function that returns a promise to be executed.
|
|---|
| 10 | * @param {number} ms - The timeout duration in milliseconds.
|
|---|
| 11 | * @returns {Promise<T>} A promise that resolves with the result of the `run` function or rejects if the timeout is reached.
|
|---|
| 12 | *
|
|---|
| 13 | * @example
|
|---|
| 14 | * async function fetchData() {
|
|---|
| 15 | * const response = await fetch('https://example.com/data');
|
|---|
| 16 | * return response.json();
|
|---|
| 17 | * }
|
|---|
| 18 | *
|
|---|
| 19 | * try {
|
|---|
| 20 | * const data = await withTimeout(fetchData, 1000);
|
|---|
| 21 | * console.log(data); // Logs the fetched data if `fetchData` is resolved within 1 second.
|
|---|
| 22 | * } catch (error) {
|
|---|
| 23 | * console.error(error); // Will log 'TimeoutError' if `fetchData` is not resolved within 1 second.
|
|---|
| 24 | * }
|
|---|
| 25 | */
|
|---|
| 26 | declare function withTimeout<T>(run: () => Promise<T>, ms: number): Promise<T>;
|
|---|
| 27 |
|
|---|
| 28 | export { withTimeout };
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.