-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcallback-hell.js
62 lines (52 loc) · 1.06 KB
/
callback-hell.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
const employees = [
{
id: 1,
firstName: 'Luke'
},
{
id: 2,
firstName: 'Tom'
},
{
id: 3,
firstName: 'Kim'
}
]
const salaries = [
{
id: 1,
salary: 3000
},
{
id: 2,
salary: 2250
}
]
const getEmployee = (id, callback) => {
const employee = employees.find((e) => e.id === id)?.firstName;
if (employee) {
callback(null, employee);
} else {
callback(`Employee with id ${id} is missing`)
}
}
const getSalary = (id, callback) => {
const salary = salaries.find((s) => s.id === id)?.salary;
if (salary) {
callback(null, salary);
} else {
callback(`Salary for id ${id} is missing`)
}
}
const id = 3;
getEmployee(id, (err, employee) => {
if (err) {
return console.error('ERROR', err);
}
getSalary(id, (err, salary) => {
if (err) {
return console.error('ERROR', err);
}
console.log('Employee', employee, 'has a salary of: ', salary);
});
});