Skip to content

Instantly share code, notes, and snippets.

@carusog
Created October 27, 2020 15:19
Show Gist options
  • Save carusog/83f3b8922eb2d0deba142e9a508b422c to your computer and use it in GitHub Desktop.
Save carusog/83f3b8922eb2d0deba142e9a508b422c to your computer and use it in GitHub Desktop.
A simple fetch example to get a subreddit json data.
fetch(`http://www.reddit.com/r/reactjs.json`)
.then(response => {
if(response.ok) {
console.log(response);
return response.json();
}
throw new Error('Request failed');
})
.then(json => {
const posts = json.data.children.map(
obj => obj.data
);
console.log(posts);
})
.catch(error => {
console.error(error);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment