Skip to content

Instantly share code, notes, and snippets.

@mohokh67
Created July 6, 2018 14:04
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 mohokh67/411fd3cb0f1540eed9ec31aa822e6539 to your computer and use it in GitHub Desktop.
Save mohokh67/411fd3cb0f1540eed9ec31aa822e6539 to your computer and use it in GitHub Desktop.
JavaScript ES6 async await
function doubleMe(name, x) {
console.log('I am running ' + name);
return new Promise(resolve => {
setTimeout(() => {
resolve(x * 2);
console.log('hoooo');
}, 2000);
});
}
function helpMe(name, time) {
console.log('I am here to help you');
return new Promise(resolve => {
setTimeout(() => {
resolve(60);
console.log('I helped you ' + name);
}, 1800);
})
}
async function doAnotherJob() {
console.log('========================================= from here');
const a = await helpMe('moho', 1000);
console.log(a);
const b = await helpMe('hoho', 80);
console.log(b);
const c = await helpMe('fery', 1000);
console.log(c);
}
async function doTheJob() {
console.log('========================================= from here');
const a = await doubleMe('moho', 10);
console.log(a);
const b = await doubleMe('hoho', 30);
console.log(b);
const c = await doubleMe('fery', 80);
console.log(c);
}
//doAnotherJob();
doTheJob();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment