Choose Snippet
Select one common JavaScript pattern
Fetch JSON
GET data from an API with error handling.
async function fetchJson(url) {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`Request failed: ${response.status}`);
}
return response.json();
}
fetchJson("https://api.example.com/users")
.then((data) => console.log(data))
.catch((error) => console.error(error));These snippets are intentionally small starting points. Copy the code, rename variables, and adapt it to your project.