JavaScriptEasy
Explain the concept of a callback function in asynchronous operations
By FrontendPro Editorial Team Updated 8/8/2026
#JavaScript
Answer
A callback function is a function passed as an argument to another function, which is then invoked inside the outer function to complete some kind of routine or action. In asynchronous operations, callbacks are used to handle tasks that take time to complete, such as network requests or file I/O, without blocking the execution of the rest of the code. For example:
js
function fetchData(callback) {
setTimeout(() => {
const data = { name: "John", age: 30 };
callback(data);
}, 1000);
}
fetchData((data) => {
console.log(data);
});
