Created
October 27, 2020 15:19
-
-
Save carusog/83f3b8922eb2d0deba142e9a508b422c to your computer and use it in GitHub Desktop.
A simple fetch example to get a subreddit json data.
This file contains hidden or 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
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