.then(...): chạy khi Promise thành công..catch(...): chạy khi có lỗi ở Promise trước đó..finally(...): luôn chạy sau cùng (dù thành công hay lỗi).
Ví dụ:
javascript
fetch(url)
.then(r => r.json())
.then(data => console.log(data))
.catch(err => console.error(err))
.finally(() => setLoading(false))Mẹo cho người mới: dùng finally để dọn trạng thái UI như loading.
.then(...): runs when the Promise succeeds..catch(...): runs when an error happens in the previous Promise chain..finally(...): always runs at the end, success or error.
Example:
javascript
fetch(url)
.then(r => r.json())
.then(data => console.log(data))
.catch(err => console.error(err))
.finally(() => setLoading(false))Beginner tip: use finally for UI cleanup, such as turning off loading state.