Skip to content

Instantly share code, notes, and snippets.

@featheredtoast
Created June 14, 2017 22:19
Show Gist options
  • Select an option

  • Save featheredtoast/3e34321092536d919221897e4383cc96 to your computer and use it in GitHub Desktop.

Select an option

Save featheredtoast/3e34321092536d919221897e4383cc96 to your computer and use it in GitHub Desktop.
quick test for async/await syntax in es7 vs pure promises
<html>
<head>
<script>
var args = {
headers: new Headers({
'Content-Type': 'text/plain'
})};
async function useAwaits() {
try {
let resp = await fetch("https://en.wikipedia.org/w/api.php?action=opensearch&format=json&origin=*&search=stack&limit=10", args);
let t = await resp.json();
return t;
} catch(e) {
return e.message;
}
}
function promisesOnly() {
return fetch("https://en.wikipedia.org/w/api.php?action=opensearch&format=json&origin=*&search=stack&limit=10", args).then(
(res) => {return res.json();},
(res) => {return res.message});
}
useAwaits().then((res) => {console.log("awaits: " + JSON.stringify(res));});
promisesOnly().then((res) => {console.log("pure promises: " + JSON.stringify(res));});
</script>
<body>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment