Created
July 19, 2018 03:06
-
-
Save SamanShafigh/4eb4bb6262e6689572bcfaf588cfe830 to your computer and use it in GitHub Desktop.
ES6 async/await
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
// 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