Modern JavaScript
About Lesson

In this JavaScript lesson we are going to learn about JavaScript Async & Await, ES8 came up with async and await keywords which can be used for asynchronous functionality in JavaScript. when an async function is called, it returns a Promise, when the async function returns a value, the Promise will be  resolved with the returned value. When the async function  throws an exception , the Promise will be rejected with the thrown value.  It is important to mention that async function works on top of  promises. they use promises to return the results. and also you need to remember that async function and await  works together. you can use await only inside async function. using it outside will throw an error. and await allows you to pause execution of async function and wait until promise is  resolved, either as fulfilled or rejected.




Multiple await




Once all await are resolved in an async function, it will be considered as completed. the first async function may have a promise or could be using a promise object and the rest will simply await till the previous one is resolved. see the example below of a promise chain.

now in here using await we are going to tell that wait for a promise to resolve or reject. It can only be used  inside an async function.



When you have multiple steps you will see the power of async or await.




Practical example