Created
October 23, 2017 14:03
-
-
Save jorgepinon/a2ffcd9a7c33083dd433f57f3c86c7fc to your computer and use it in GitHub Desktop.
Promise with async/await structure
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
/* 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