Future: `await` & `async`

This lesson covers the usage of `await` & `async` to fetch results asynchronously using `Future` objects.

In this lesson, you’ll learn to use the await and async keywords to carry out asynchronous operations.

The code that uses async and await keywords is asynchronous, but it looks appears similar to synchronous code.

In this lesson, you will see a few examples that use await to wait for the result of an asynchronous function to return before executing the next line of code.

The await & async

The await and async keywords are used together. The function that is expected to perform the expensive work will be marked with the keyword async. Inside the function, the expensive call is prefixed by the keyword await. The program will suspend when await is called or a function returns or it reaches the end of the function.

With the following code snippet, we will see how async and await keywords are used.

await can only be called in a function that is marked/declared as async.

Get hands-on with 1400+ tech skills courses.