Skip to content

Instantly share code, notes, and snippets.

@wklsh
Last active July 13, 2021 18:20
Show Gist options
  • Save wklsh/346a73a582b2e58031c5eae985fe4ee6 to your computer and use it in GitHub Desktop.
Save wklsh/346a73a582b2e58031c5eae985fe4ee6 to your computer and use it in GitHub Desktop.
Async / await API example for dummies

Async / await API example for dummies

Extracted from MDN Web Doc - Making asynchronous programming easier with async and await

index.js

async function myFetch() {
  let response = await fetch('coffee.jpg');
  if (!response.ok) {
    throw new Error(`HTTP error! status: ${response.status}`);
  }
  return await response.blob();

}

myFetch().then((blob) => {
  let objectURL = URL.createObjectURL(blob);
  let image = document.createElement('img');
  image.src = objectURL;
  document.body.appendChild(image);
}).catch(e => console.log(e));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment