Awaiting the result of async functions

In the previous recipe, we saw how to use async to create functions that resolve promises. However, we used the Promise API then callback to wait for the result. In many situations, we can use the await key word to wait for these values. It gets the job done, but there is a cleaner way to retrieve results from asynchronous functions.

This recipe demonstrates how to use await in order to return the result of a long-running operation.

Get ECMAScript Cookbook 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.