Skip to content

Instantly share code, notes, and snippets.

@jorgepinon
Created October 23, 2017 14:03
Show Gist options
  • Save jorgepinon/a2ffcd9a7c33083dd433f57f3c86c7fc to your computer and use it in GitHub Desktop.
Save jorgepinon/a2ffcd9a7c33083dd433f57f3c86c7fc to your computer and use it in GitHub Desktop.
Promise with async/await structure
/* thanks to @mathias for this. https://twitter.com/mathias/status/922460187671216129 */
const fetchAndDisplay = async function(url, element) {
showLoadingSpinner();
try {
const response = await fetch(url);
const text = await response.text();
element.textContent = text;
}
catch(error) {
element.textContent = error.message;
}
finally {
hideLoadingSpinner();
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment