Skip to content

Instantly share code, notes, and snippets.

@SastraNababan
Created October 30, 2017 19:38
Show Gist options
  • Save SastraNababan/17e7fd24388317a2871adcf4f4e67587 to your computer and use it in GitHub Desktop.
Save SastraNababan/17e7fd24388317a2871adcf4f4e67587 to your computer and use it in GitHub Desktop.
Promise Chain Example 2
const getPost = () => fetch('https://jsonplaceholder.typicode.com/posts/1')
const getAuthor = (id) => fetch('https://jsonplaceholder.typicode.com/users/' + id)
const getComment = (id) => fetch('https://jsonplaceholder.typicode.com/users/' + id)
var a = getPost().then(res => res.json()) // #1 get post
var b = a.then(res => getAuthor(res.id)).then(res => res.json()) // #2 get author
var c = a.then(res => getComment(res.id)).then(res => res.json()) //#3 get comment
Promise.all([a,b,c]).then(results => {
console.log(results[0])
console.log(results[1])
console.log(results[2])
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment