The async keyword makes a function asynchronous and the return value of the function is a Promise. The function does not require Promise-related code in the body of the function in order to return a Promise. Async works in conjunction with await because you can only use the await keyword within functions that have been declared with the async keyword. Let’s look at an example of async in action!
If you look at the example, the readList function uses the async keyword and the return value is a Promise. Now, the only way to fulfill the Promise is to use the await keyword!
The await keyword takes a Promise and turns it back into a return value or throw an exception. When given a Promise object, the await expression waits until the Promise is settled. If the promise is fulfilled, the fulfillment value of the Promise will be returned and if the promise is rejected, the rejection value will be thrown. The most important thing to remember about the await keyword is that it does not cause a program to block and do nothing until a Promise is settled, the code actually remains asynchronous and the await simply disguises this fact. This means that any code that uses await is itself asynchronous. This is why, as I stated earlier, the await keyword can only be used within an asynchronous function. Let’s look at an example using our readList function from earlier!
If you look at the example, I included the await keyword in the readList function that was made asynchronous using the async keyword. If you look at the console, you’ll see that the Promise that was returned is pending, which is a good example of my earlier point that await does not block a function from returning a value, the function is still asynchronous, it’s just hiding that fact.