Created
March 8, 2018 23:42
-
-
Save featheredtoast/1f06afa8f8731045ca30c43459115e69 to your computer and use it in GitHub Desktop.
promise vs await tests
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
<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); | |
return await resp.text(); | |
} 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.text();}, | |
(res) => {return res.message}); | |
} | |
function myPromise() { | |
var p = new Promise((resolve, reject) => {resolve("welp");}); | |
return p.then(res=>{return "welp number twooo";}); | |
} | |
useAwaits().then((res) => { | |
console.log("awaits: " + JSON.stringify(res)); | |
document.getElementById("awaits").innerHTML = res; | |
}); | |
promisesOnly().then((res) => { | |
console.log("pure promises: " + JSON.stringify(res)); | |
document.getElementById("promises").innerHTML = res; | |
}); | |
myPromise().then(res=>{console.log("mypromise:" + res);}) | |
</script> | |
<body> | |
<h2>Awaits...</h2> | |
<div id="awaits"></div> | |
<h2>Promises...</h2> | |
<div id="promises"></div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment