Promises, Async/Await, Callback
🔎 async function là gì?
Callback
Cách cũ (ES5 – dùng callback hoặc Promise.then)
Promise là cách tiếp cận xử lý async và callback.
A Promise is an object that represents the eventual result of an asynchronous operation.
It can be in one of three states:
- Pending – the async operation hasn’t finished yet.
- Fulfilled – the operation succeeded (calls resolve).
- Rejected – the operation failed (calls reject).
More often than not, you will be using Promises when working with APIs rather than creating them yourself. However, it is important to understand how they work.
Không dùng Arrow Function
Dùng Arrow Function
Cách mới (ES8 – async/await)
👉 Kết quả giống nhau, nhưng async/await dễ đọc, dễ debug hơn.
The `async` keyword is used to define an asynchronous function, and the `await` keyword is used to wait for the Promise to resolve. We can use `try` and `catch` blocks to handle the resolved and rejected Promises.
