Skip to content

Instantly share code, notes, and snippets.

@jrrio
Last active December 25, 2021 02:45
Show Gist options
  • Save jrrio/3d7f8c177551d4bb2032282924a976fb to your computer and use it in GitHub Desktop.
Save jrrio/3d7f8c177551d4bb2032282924a976fb to your computer and use it in GitHub Desktop.
Fetch wrapper
async function getData(url) {
try {
const response = await fetch(url);
if (!response.ok) throw new Error(`${response.status}: ${response.statusText}`);
const data = await response.json();
return data;
} catch (error) {
console.error(error);
}
}
// getData returns a Promise, so...
getData("https://jsonplaceholder.typicode.com/posts/1").then((data) =>
console.log(data)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment