Skip to content

Instantly share code, notes, and snippets.

@priteshjha4u
Last active July 4, 2019 13:42
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 priteshjha4u/a0573f5278d24f75daa0c87bb0b422a3 to your computer and use it in GitHub Desktop.
Save priteshjha4u/a0573f5278d24f75daa0c87bb0b422a3 to your computer and use it in GitHub Desktop.
async await es2017 simple example
//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