Callback là hàm truyền vào hàm khác để được gọi sau.
Ví dụ:setTimeout(() => console.log("xong"), 1000)
Hàm arrow bên trong là callback.
Vì JavaScript chạy một luồng (single thread), callback giúp:
- không chặn app khi chờ API hoặc file
- làm việc bất đồng bộ (async)
Hiện nay ta thường viết async bằng Promise và async/await, nhưng callback vẫn là nền tảng cần hiểu.
A callback is a function passed into another function to be called later.
Example:setTimeout(() => console.log("done"), 1000)
The arrow function is the callback.
Because JavaScript is single threaded, callbacks help:
- avoid blocking while waiting for API or file work
- enable asynchronous behavior
Today we usually write async flows with Promise and async/await, but callbacks are still foundational.