-
Notifications
You must be signed in to change notification settings - Fork 0
/
callback.js
143 lines (106 loc) · 3.32 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// A callback is a function that is passed as an argument to another function
// and is executed after some operation has been completed
// What is the problem?
// 1. In case of multiple asynchronous operations, the code becomes nested and difficult to read
// code example of multiple asynchronous operations:
// example without callback function:
// breaking the DRY principle
// challenges solved by callback functions:
// 1. code duplication, example:
// 2. nested code
// 3. callback hell
const nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
console.log(nums);
function copyArrayAndMultiplyByTwo(array) {
const output = [];
for (let i = 0; i < array.length; i++) {
output.push(array[i] * 2);
}
return output;
}
function copyArrayAndDivideByTwo(array) {
const output = [];
for (let i = 0; i < array.length; i++) {
output.push(array[i] / 2);
}
return output;
}
function copyArrayAndAddTwo(array){
const output = [];
for(let i = 0; i < array.length; i++){
output.push(array[i] + 2);
}
return output;
}
// =======================================================================
// using the callback functions:
function multiplyByTwo(number) {
return number * 2;
}
function divideByTwo(number) {
return number / 2;
}
function addTwo(number) {
return number + 2;
}
function copyArray(array, instructions) {
const newArray = [];
array.forEach((element) => {
newArray.push(instructions(element));
});
return newArray;
}
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
console.log(numbers);
const newNumbers = copyArray(numbers, multiplyByTwo);
console.log(newNumbers);
const newNumbers2 = copyArray(numbers, divideByTwo);
console.log(newNumbers2);
const newNumbers3 = copyArray(numbers, addTwo);
console.log(newNumbers3);
// You can pass annonymous functions as arguments to another function
// - You don't need to create a new function for each operation
// - You can pass a annonymous/arrow function as an argument to another function
// Passing annonymous function as arguments to another function
const newNumbers4 = copyArray(numbers, function (number) {
return number + 2;
});
console.log(newNumbers4);
// Passing arrow function as arguments to another function
const newNumbers5 = copyArray(numbers, (number) => number + 2);
console.log(newNumbers5)
// higher order function
// higher order function is: a function that takes a function as an argument
// or returns a function as a result
// A “higher-order function” is a function that accepts functions as parameters
// and/or returns a function.
const newNumbers6 = numbers.map((number) => number * 2);
console.log(newNumbers6);
// example of a function that returns a function as a result:
function createAdder(num) {
return function (x) {
return x + num;
};
}
const addTwo = createAdder(2);
console.log(addTwo(10));
const addFive = createAdder(5);
console.log(addFive(10));
// =======================================================================
function validateMoney(money) {
const interest = 100;
if (money > 0) {
return money + interest;
} else {
return money;
}
}
function getInterestMoney(money, callback) {
if (typeof money !== "number") {
return callback("money is not a number");
} else {
return callback(money);
}
}
const money = getInterestMoney(1200, validateMoney);
console.log(money);