Callback Function
A callback function is a function that is passed as an argument to another function and is intended to be called when a specific event occurs or when a particular task is completed. Callback functions are a fundamental concept in event-driven and asynchronous programming.
Passing a Function: You provide a function (the callback) as an argument to another function (often called the "parent" or "caller" function).
Execution Control: The parent function carries on with its execution, and at some point, it triggers or calls the callback function based on certain conditions or events.
Custom Behavior: The
callback functionis designed to perform a specific action or set of actions. When it's called by the parent function, it executes those actions.
Example :
function sayHello(){
console.log('Hello');
}
function useEffect(fn, arr){
fn(); // Here, we're calling the function passed as 'fn', which is sayHello
console.log(arr); // Here, we're logging the 'arr' array to the console
}
useEffect(sayHello, ['a', 'b', 'c']);
sayHelloFunction: This is a simple function that logs "Hello" to the console when called.useEffectFunction: It takes two parameters:fn(Function): This parameter is a callback function. In this case, it's thesayHellofunction, which is the function to be executed.arr(Array): This parameter is an array of values, which will be logged to the console.
Inside
useEffect:- It calls the
fnfunction, which is the callback functionsayHello. This is where the callback function is used. The callback function is executed whenuseEffectis called. - It logs the contents of the
arrarray to the console.
Calling
useEffect: TheuseEffectfunction is called with two arguments:sayHello: This is the callback function that will be executed.['a', 'b', 'c']: This is an array that will be logged to the console.
So, in this example,
sayHello is the callback function because it's passed as an argument to the useEffect higher-order function.
useEffect is the higher-order function because it takes a callback function (fn) as one of its arguments.
- Calls the
sayHellofunction, which logs "Hello" to the console. - Logs the array
['a', 'b', 'c']to the console.
This demonstrates the use of a callback function (sayHello) within the useEffect function.
Callbacks are commonly used in scenarios where you want to specify a custom action to be executed within another function at a specific point in its execution. In this example, sayHello is the callback function that gets executed within useEffect.