Created
September 2, 2016 00:08
-
-
Save rafeca/b30675b4b7338e920d4c740a204b21a9 to your computer and use it in GitHub Desktop.
Last example of https://www.youtube.com/watch?v=RRgAdi3gX-s&feature=youtu.be using async/await
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 fetchData() { | |
try { | |
const response = await fetch('https://api.spotify.com/v1/search?q=michael+jackson&type=album'); | |
const parsedResponse = await response.json(); | |
const ids = parsedResponse.albums.items | |
.map(item => item.id) | |
.slice(0, 5); | |
for (let id of ids) { | |
const albumResponse = await fetch('https://api.spotify.com/v1/albums/' + id); | |
const albumData = await albumResponse.json(); | |
const albumImage = await fetch(albumData.images[0].url); | |
const imageBlob = await albumImage.blob(); | |
const imageURLData = URL.createObjectURL(imageBlob); | |
const image = await createImage(imageURLData); | |
document.body.appendChild(image); | |
} | |
} catch (e) { | |
console.log('Error!!', e); | |
} | |
} | |
async function createImage(imageURLData) { | |
return new Promise(resolve => { | |
const image = new Image(); | |
image.addEventListener('load', () => { | |
resolve(image); | |
}); | |
image.src = imageURLData; | |
}); | |
} | |
fetchData().then(() => console.log('Done!')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment