Created
August 21, 2016 17:20
-
-
Save udnisap/ac38a057eecd2da11741b94014272794 to your computer and use it in GitHub Desktop.
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
const API = 'https://jsonplaceholder.typicode.com'; | |
// Data Fetching and Logic | |
const getData = (endpoint) => | |
fetch(`${API}/${endpoint}`) | |
.then(resp => resp.json()); | |
const findUserByUserName = (username) => | |
getData(`users?username=${username}`) | |
.then(users => users[0]); | |
const getPostsUser = (userId) => getData(`posts?userId=${userId}`); | |
const getPostsByUserName = (username) => | |
findUserByUserName(username) | |
.then(user => getPostsUser(user.id)); | |
//Execution | |
console.log('Application Starts'); | |
console.log('------------------'); | |
console.log('Posts from Bret are'); | |
const postsPromise = getPostsByUserName('Bret'); | |
postsPromise.then(posts => { | |
posts.forEach((post, i) => console.log(`${i}. ${post.title}`)); | |
}); | |
console.log('Application Finishes'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment