-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcallback.js
77 lines (64 loc) · 2.9 KB
/
callback.js
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// To run this script: node callback
// Import axios
const axios = require("axios");
const fs = require("fs");
// Callback functions are functions passed as an argument to another function
// Functions which accepts other functions as argument or returning a function are called Higher Order Functions
// Let's first define a Higher Order Function (Because it will take another function as argument)
// This function will take a number and a callback function as parameter and execute the callback inside it's body
function higherOrderFunction(number, callback) {
console.log("higherOrderFunction started executing");
callback(number);
console.log("higherOrderFunction finished executing");
}
// Let's define our callback function to add numbers between 1 to number
function add(number) {
console.log("callback function started executing");
let total = 0;
for (let i = 1; i <= number; i++) {
total += i;
}
console.log(`callback function finished executing, TOTAL: ${total}`);
}
// Let's call our Higher Order Function with a number and add function as a callback
higherOrderFunction(100, add);
// Result will be:
// higherOrderFunction started executing
// callback function started executing
// callback function finished executing, TOTAL: 5050
// higherOrderFunction finished executing
// But this example seems useless. Where can i really use this ? Let's see.
// We have a function which takes an object and returns the stringified version of the object
function stringifyObject(object) {
console.log(JSON.stringify(object));
}
// We have another function which makes an API call and returns an object as a result
// function apiCall() {
// axios
// .get("https://jsonplaceholder.typicode.com/todos/1")
// .then((response) => response.data);
// }
// We want to get the data first with apiCall function and stringify the returned object. How can we do that ? Let's modify
// our apiCall function to make it take a callback
// We will wait for the data first and then call callback function with that data
function apiCall(callback) {
axios.get("https://jsonplaceholder.typicode.com/todos/1").then((response) => {
callback(response.data);
});
}
apiCall(stringifyObject); // Result will be: {"userId":1,"id":1,"title":"delectus aut autem","completed":false}
// Let define another function to use as a callback in apiCall, which will return keys in the object
function returnKeys(object) {
console.log(Object.keys(object));
}
apiCall(returnKeys); // Result will be: [ 'userId', 'id', 'title', 'completed' ]
// Another example with asynchronous fs.readFile() method
// Here we write our callback function directly as an argument and it will be called after reading file
// This will probably execute faster than apiCall function because apiCall makes network request which is slower
fs.readFile("./README.md", (error, data) => {
if (error) {
console.log(error.message);
} else {
console.log(data);
}
});