Last active
July 4, 2019 13:42
-
-
Save priteshjha4u/a0573f5278d24f75daa0c87bb0b422a3 to your computer and use it in GitHub Desktop.
async await es2017 simple example
This file contains hidden or 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
//run this example function in chrome console to see async await in action | |
async function asyncAwaitExample() { | |
let tmp = 1; | |
let value1 = await getValueFromApi(2000); | |
let value2 = await getValueFromApi(4000); | |
//this function is here just to simulate a server/api behavior | |
//returns a random value after provided time(in ms) | |
function getValueFromApi(t) { | |
return new Promise((rs, rj) => { setTimeout(v => { console.log(`getvalue call ${tmp} ---- ${v}`); tmp++; rs(v + v) }, t, Math.random().toString(36).substring(7)) }).then(v => v + v); | |
} | |
//print the values on console | |
console.log(`${value1 + value2}`); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment