Skip to content

Instantly share code, notes, and snippets.

@SamanShafigh
Created July 19, 2018 03:06
Show Gist options
  • Save SamanShafigh/4eb4bb6262e6689572bcfaf588cfe830 to your computer and use it in GitHub Desktop.
Save SamanShafigh/4eb4bb6262e6689572bcfaf588cfe830 to your computer and use it in GitHub Desktop.
ES6 async/await
// We can also use async/await which is a Syntactic Sugar For Promises
var addC = function (a, b) {
return new Promise((resolve, reject) => {
setTimeout (() => {
resolve(a + b);
}, 0);
});
};
// your code using async functions is much more like using standard synchronous functions
// Note you should only use `await` inside a function that is declared as `async function`
async function main () {
var r1 = await addC(1, 1);
var r2 = await addC(r1, 1);
var r3 = await addC(r2, 1);
console.log(r3);
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment