Created
October 30, 2017 19:39
-
-
Save SastraNababan/1dd780a08e529cee70825bcda7fb0989 to your computer and use it in GitHub Desktop.
Promise All Example
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 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