-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcallback-function.js
More file actions
41 lines (31 loc) · 1.34 KB
/
Copy pathcallback-function.js
File metadata and controls
41 lines (31 loc) · 1.34 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
A callback is a function that is passed as an argument to another function, and is called after the main function has finished its execution.
The main function is called with a callback function as its argument, and when the main function is finished, it calls the callback function to provide a result.
Callbacks allow you to handle the results of an asynchronous operation in a non-blocking manner, which means that the program can continue to run while the operation is being executed.
function mainFunction(callback) {
return callback();
}
function callbackFunction() {
console.log("Hello, I am the callback function");
}
mainFunction(callbackFunction);
// example 2
// Simulating an asynchronous operation with a callback
const readFileAsync = (filePath, callback) => {
setTimeout(() => {
const fileContent = `Content of ${filePath}`;
// Simulating a successful operation
callback({ error: null, content: fileContent });
// To simulate an error, you can uncomment the following line
// callback({ error: 'Error reading file', content: null });
}, 1000);
};
// Separate callback function
const handleFileResult = ({ error, content }) => {
if (error) {
console.error(`Error: ${error}`);
} else {
console.log(`File Content: ${content}`);
}
};
// Example usage
readFileAsync('/path/to/file.txt', handleFileResult);