Skip to content

Instantly share code, notes, and snippets.

@rafeca
Created September 2, 2016 00:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rafeca/b30675b4b7338e920d4c740a204b21a9 to your computer and use it in GitHub Desktop.
Save rafeca/b30675b4b7338e920d4c740a204b21a9 to your computer and use it in GitHub Desktop.
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