- ECMAScript Cookbook
- Ross Harrison
- 152字
- 2021-08-27 19:27:45
How it works...
The async keyword instructs the run-time that this function returns a Promise, rather than a result directly. By looking at the log messages, you can clearly see the order of operations. The first message is logged before the async function call is made. Next, the message inside of the async function is logged. Then the message after the async function is called. And finally, the message inside of the then callback is logged. This order is the same order of execution that can be seen in code using the Promise API directly.
The preceding code is already an improvement over code that creates promises directly. In upcoming recipes, we'll see how to take advantage of await to retrieve results from these functions without the use of the Promise API. We'll also be looking at other situations where async functions provide an advantage over direct use of the Promise API.