-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcallback.js
More file actions
74 lines (51 loc) · 1.24 KB
/
Copy pathcallback.js
File metadata and controls
74 lines (51 loc) · 1.24 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
function greet(name){
console.log(`Hello ${name}`);
}
function processUser(callback){
const name = "partha sarker";
callback(name)
}
processUser(greet)
// callback with array map method
const number = [1, 2, 3, 4, 5, 6];
// pass arrow function inside map
const doubled = number.map(num =>{
return num * 2;
})
console.log(doubled);
// pass regular function as a argumanet
function nMultipy(n) {
return n * 2;
}
const newDoubleArray = number.map(nMultipy);
console.log(newDoubleArray);
// filter method
const eventNumber = number.filter(num => num % 2 === 0)
console.log("even number : ", eventNumber);
// three important callback type
// 1. Synchronous callback
[1,2,3].map(x=> x*2)
// 2. Asynchronous callback
setTimeout(() => {
console.log("Done");
}, 1000);
// 3. Error-first callback
function callback(error, result) {
if (error) {
// handle error
return;
}
// use result
}
// A callback is a function passed to another function so that it can be invoked later or durign some operation
// Callback
// │
// ├── synchronous
// │
// └── asynchronous
// │
// Event Loop
// │
// Promise
// │
// async / await