Last active
December 25, 2021 02:45
-
-
Save jrrio/3d7f8c177551d4bb2032282924a976fb to your computer and use it in GitHub Desktop.
Fetch wrapper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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