Skip to content

Instantly share code, notes, and snippets.

@TravelingTechGuy
Created October 21, 2017 19:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TravelingTechGuy/2bf7e58c071cf856094c0060fa5d9c45 to your computer and use it in GitHub Desktop.
Save TravelingTechGuy/2bf7e58c071cf856094c0060fa5d9c45 to your computer and use it in GitHub Desktop.
Use `fetch` with `async` and `await`
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