Created
September 13, 2019 10:40
-
-
Save novonimo/4cf07a6c400fecaf9da7f37e24c6dff6 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
async function fetchAvatarUrl(userId) { | |
const response = await fetch(`https://catappapi.herokuapp.com/users/${userId}`) | |
const user = await response.json(); | |
return Promise.all(user.cats.map(async (catId) => { | |
const response = await fetch(`https://catappapi.herokuapp.com/cats/${catId}`) | |
const catData = await response.json() | |
return catData.imageUrl | |
})) | |
} | |
const result = fetchAvatarUrl(123) | |
console.log(result); |
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
function fetchAvatarUrlWithPromise(userId) { | |
return fetch(`https://catappapi.herokuapp.com/users/${userId}`) | |
.then(response => response.json()) | |
.then(data=>data.imageUrl) | |
} | |
async function fetchAvatarUrlWithAsync(userId) { | |
const response = await fetch(`https://catappapi.herokuapp.com/users/${userId}`) | |
const data = await response.json(); | |
return data.imageUrl | |
} | |
const result = fetchAvatarUrlWithPromise(123) | |
console.log(result); | |
const asyncResult = fetchAvatarUrlWithAsync(123) | |
console.log(asyncResult); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment