Promise.prototype.finally(callback) chạy callback khi promise **settled** (fulfilled HOẶC rejected). Trả về Promise mới.
Ví dụ thành công
new Promise(resolve => setTimeout(() => resolve("success"), 1000))
.then(val => console.log(val))
.finally(() => console.log("Promise complete!"));
success Promise complete!
Ví dụ thất bại
new Promise(() => undefined.get()) // throw error
.then(val => console.log(val))
.catch(err => console.log("Failed!"))
.finally(() => console.log("Promise complete!"));
Failed! Promise complete!
Use case
- Cleanup: loading spinner, close connection, release lock.
- Chạy code bất kể thành công/thất bại — tránh duplicate logic trong then/catch.