-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path08_call_back_function.js
More file actions
54 lines (40 loc) · 2.59 KB
/
08_call_back_function.js
File metadata and controls
54 lines (40 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Topic: Call back function
// Q. What is callback function?
// -> A callback function is a function that is passed as an argument to another function. The main function can then call the callback function at a specific time, allowing you to control the sequence of actions in your code.
// Q. Why use callback function?
// -> Callbacks are essential in JavaScript because they are the primary mechanism for asynchronous operations and event handling, allowing JavaScript to be non-blocking.
// -> Asynchronicity: They tell a function what to do after a long operation (like fetching data, timing out, or waiting for a file to load) is complete, preventing the program from freezing.
// -> Customization: They allow a general-purpose function (like map, filter, or an event listener) to execute custom instructions defined by the developer on a specific set of data or event.
// Q. How to use callback function?
// The process involves two steps:
// -> Definition: The developer defines the function to be executed later (the callback) and passes it to the higher-order function.
// -> Invocation: The higher-order function executes its main logic, and when it is ready, it explicitly calls the callback function, usually passing in any relevant data (like an array element, or an error/result).
// Note:
// -> The function that receives the callback is called the Higher-Order Function.
// Syntax:
// Passing regular function as callback:
function higherOrderFunction(callback) {
// Do something...
console.log("I am higher order function");
callback();
}
function callbackFunction() {
// Do something after the higher-order function completes its task
console.log("I am callback function");
}
higherOrderFunction(callbackFunction);
// Passing Arrow Function as callback:
// -> The callback is passed in where any other argument would go, often as an anonymous Arrow Function for conciseness.
higherOrderFunction(() => "I am callback arrow function");
// Example:
// 1. Higher-Order Function: setTimeout
// 2. Callback Function: The arrow function () => { ... }
console.log("Start");
setTimeout(() => {
// This function is the callback. It runs *after* the 2000ms delay.
console.log("Executed later.");
}, 2000);
console.log("End");
// Output: Start, End, ... (2 seconds later) Executed later.
// Note
// -> Callbacks are fundamental in JavaScript, especially for asynchronous tasks. However, using too many nested callbacks can lead to complex and hard-to-read code. To handle asynchronous operations more elegantly, consider using Promises or Async/Await, which provide a more structured way to work with asynchronous code.