Skip to content

Instantly share code, notes, and snippets.

@novonimo
Created September 13, 2019 10:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save novonimo/4cf07a6c400fecaf9da7f37e24c6dff6 to your computer and use it in GitHub Desktop.
Save novonimo/4cf07a6c400fecaf9da7f37e24c6dff6 to your computer and use it in GitHub Desktop.
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);
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