Optimize Your Promise by Using Async/Await in React
Bunlong
React Patterns TeamWhen Promise was introduced in ES5, it was meant to solve a problem with asynchronous code, and they did, but over the 2 years that separated ES5 and ES7, it was clear that promises could not be the final solution.
Promise was introduced to solve the famous callback hell problem, but they introduced complexity on their own, and syntax complexity.
How can we escape Promise syntax complexity? Good things come to those who Await.
#
Escape Syntax Complexity#
Without Using Async/AwaitDon't over complicate your code and save time using Async/Await.
#
Using Async/Await#
Why Async/Await?- Added in ES6 as a native implementation by default
- Make your code cleaner
- Make your request and code maintenance easier
- Reduce .catch to handle errors
#
How to Use it?Create a function with async keyword.
Use await inside an async function only.