How it works...

Because our main function is marked as async, we are able to use the await operator in its body. This operator causes the function to wait for the result of the expression to be resolved. In our case, this means the Promise created by the async function we called is fulfilled.

Once the result is fulfilled, the control flow continues to the next statement. If we did not use the await operator, the value of randomNumber would be a promise that would resolve to the returned value. We could handle this with the promise interface, but because we used await , we are able to write something that looks more like synchronous code.

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.