Skip to content

Instantly share code, notes, and snippets.

@lethalbrains
Created July 12, 2017 11:17
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 lethalbrains/e8dcb97b9ba36ddb53265be8562edf82 to your computer and use it in GitHub Desktop.
Save lethalbrains/e8dcb97b9ba36ddb53265be8562edf82 to your computer and use it in GitHub Desktop.
// Current syntax
function fetchJson(url) {
return fetch(url)
.then(request => request.text())
.then(text => {
return JSON.parse(text);
})
.catch(error => {
console.log(`ERROR: ${error.stack}`);
});
}
// New async functions
async function fetchJson(url) {
try {
let request = await fetch(url);
let text = await request.text();
return JSON.parse(text);
}
catch (error) {
console.log(`ERROR: ${error.stack}`);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment