Created
October 21, 2017 19:10
-
-
Save TravelingTechGuy/2bf7e58c071cf856094c0060fa5d9c45 to your computer and use it in GitHub Desktop.
Use `fetch` with `async` and `await`
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
let getJson = async url => { | |
try { | |
let response = await fetch(url); | |
let json = await response.json(); | |
return json; | |
} | |
catch(ex) { | |
//throw ex; //may want to handle externally | |
console.error(ex); | |
} | |
}; | |
//demo | |
let now = getJson('http://date.jsontest.com/'); | |
console.log(now.time); //should show current time |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment