The async function and await keyword

In order to use the await keyword, we need to have an async function. The difference between a function and an async function is that the async function is followed by an async keyword. Let's take a look at an example:

async function ES8isCool() {   // asynchronous work   const information = await getES8Information() // Here getES8Information itself is an async function}

This is the crux of the thing. You can only use await inside an async function. This is because, when you call an async function, it returns a Promise. However, instead of using then with it, which eventually makes it a promise chain, we use the await keyword in front of it to kind of pause the execution (not really) inside the async function ...

Get Learn ECMAScript - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.