Created
March 3, 2018 18:22
-
-
Save andyshora/65e2e4d1e0c590131c5a38c15ecf5915 to your computer and use it in GitHub Desktop.
Async Await Example
This file contains hidden or 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 fetchConfig() { | |
const response = await fetch( | |
`${TMDB_API_PATH}/configuration?api_key=${TMDB_API_KEY}`, | |
); | |
return await response.json(); | |
} | |
// Async/Await requirements: Latest Chrome/FF browser or Babel: https://babeljs.io/docs/plugins/transform-async-to-generator/ | |
// Fetch requirements: Latest Chrome/FF browser or Github fetch polyfill: https://github.com/github/fetch | |
// async function | |
async function fetchAsync () { | |
// await response of fetch call | |
let response = await fetch('https://api.github.com'); | |
// only proceed once promise is resolved | |
let data = await response.json(); | |
// only proceed once second promise is resolved | |
return data; | |
} | |
// trigger async function | |
// log response or catch error of fetch promise | |
fetchAsync() | |
.then(data => console.log(data)) | |
.catch(reason => console.log(reason.message)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment