1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
async function fetchWithTimeout(timeout = 2_000) {
const abortSignal = AbortSignal.timeout(timeout);
try {
const response = await fetch("https://jsonplaceholder.typicode.com/posts/1", {
signal: abortSignal,
});
return await response.json();
} catch (error) {
if (error instanceof DOMException && error.name === "TimeoutError") {
// Handle request timeout error
console.error("Request timed out", error);
} else { /* Handle other errors */ }
}
}
fetchWithTimeout(2_000).then(console.log);
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
May 3, 2024