Tip 44: Create Clean Functions with Async/Await
Understand how to write cleaner asynchronous JavaScript functions by using async and await. Learn to refactor promise chains into simpler code, handle errors efficiently, and improve the readability of your asynchronous workflows.
We'll cover the following...
In the previous tip, you saw that promises are awesome. They’re a vast improvement over callbacks, but their interfaces are still a little clunky. You’re still working with callbacks in methods. Fortunately, the language continues to improve. You can now avoid callbacks entirely by adding asynchronous promise data to a variable in a single function.
async & await
Developers usually talk about the new syntax, async/await, as a group, but it’s really two separate actions. You use the async keyword to declare that an
encapsulating function will be using asynchronous data. Inside the asynchronous function, you can use the await keyword ...