QuickSnip
Browse
Tags
+ New
← Back to snippet
Edit snippet
Title
Description
(optional)
Abort a fetch request if it takes longer than the given timeout.
Language
JavaScript
TypeScript
JSX
TSX
Python
Go
Rust
Bash
SQL
CSS
HTML
JSON
YAML
Markdown
Tags
Loading tags…
Code
export async function fetchWithTimeout(url, ms = 5000, options = {}) { const controller = new AbortController(); const timer = setTimeout(() => controller.abort(), ms); try { const res = await fetch(url, { ...options, signal: controller.signal }); return await res.json(); } finally { clearTimeout(timer); } }
Update snippet
Cancel