Skip to content

Instantly share code, notes, and snippets.

@udnisap
Created August 21, 2016 17:20
Show Gist options
  • Save udnisap/ac38a057eecd2da11741b94014272794 to your computer and use it in GitHub Desktop.
Save udnisap/ac38a057eecd2da11741b94014272794 to your computer and use it in GitHub Desktop.
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