|
1 | 1 | import { AbortSignal } from "abort-controller"
|
2 | 2 |
|
3 |
| -/** |
4 |
| -Create an AbortSignal that aborts after a delay. |
5 |
| -@param timeout The milliseconds to wait. |
6 |
| -@example |
7 |
| -``` |
8 |
| -const timeoutSignal = require("timeout-signal"); |
9 |
| -const fetch = require("cross-fetch"); |
| 3 | +declare const timeoutSignal: { |
| 4 | + /** |
| 5 | + Create an AbortSignal that aborts after a delay. |
| 6 | + @param timeout The milliseconds to wait. |
| 7 | + @example |
| 8 | + ``` |
| 9 | + const timeoutSignal = require("timeout-signal"); |
| 10 | + const fetch = require("cross-fetch"); |
10 | 11 |
|
11 |
| -fetch("https://www.google.com", { signal: timeoutSignal(5000) }) |
12 |
| - .then(response => { |
13 |
| - // Handle response |
14 |
| - }) |
15 |
| - .catch(error => { |
16 |
| - if (error.message === "The user aborted a request.") { |
17 |
| - // Handle abortion |
18 |
| - } |
19 |
| - }) |
20 |
| -``` |
21 |
| -*/ |
22 |
| -declare function timeoutSignal(timeout: number): AbortSignal |
| 12 | + fetch("https://www.google.com", { signal: timeoutSignal(5000) }) |
| 13 | + .then(response => { |
| 14 | + // Handle response |
| 15 | + }) |
| 16 | + .catch(error => { |
| 17 | + if (error.message === "The user aborted a request.") { |
| 18 | + // Handle abortion |
| 19 | + } |
| 20 | + }) |
| 21 | + ``` |
| 22 | + */ |
| 23 | + (timeout: number): AbortSignal |
| 24 | + |
| 25 | + /** |
| 26 | + The signal to clear the timeout for. |
| 27 | + @param signal The signal to clear the timeout for. |
| 28 | + @example |
| 29 | + ``` |
| 30 | + const timeoutSignal = require("timeout-signal"); |
| 31 | + const fetch = require("cross-fetch"); |
| 32 | +
|
| 33 | + const signal = timeoutSignal(5000) |
| 34 | +
|
| 35 | + fetch("https://www.google.com", { signal }) |
| 36 | + .then(response => { |
| 37 | + timeoutSignal.clear(signal) |
| 38 | + // Handle response |
| 39 | + }) |
| 40 | + .catch(error => { |
| 41 | + if (error.message === "The user aborted a request.") { |
| 42 | + // Handle abortion |
| 43 | + } |
| 44 | + }) |
| 45 | + ``` |
| 46 | + */ |
| 47 | + clear(signal: AbortSignal): void |
| 48 | +} |
23 | 49 |
|
24 | 50 | export = timeoutSignal
|
0 commit comments