forked from TrainingByPackt/Professional-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathActivity11.js
More file actions
79 lines (77 loc) · 2.41 KB
/
Activity11.js
File metadata and controls
79 lines (77 loc) · 2.41 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
75
76
77
78
79
const clientApi = {
getRate: (id, callback) => {
setTimeout(() => {
switch (id) {
case 'DDW2AU':
callback(null, 1.23);
break;
case 'DV50PD':
callback(null, 0.9);
break;
case 'WCGL6F':
callback(null, 1.6);
break;
default:
callback(new Error ('not found'))
}
}, 100);
},
getUsers: (callback) => {
setTimeout(() => {
callback(null, {
users: [
{
id: 'DDW2AU',
address: '4560 Hickman Street'
},
{
id: 'DV50PD',
address: '3445 Red Hawk Road'
},
{
id: 'WCGL6F',
address: '2355 University Hill Road'
}
]
});
}, 1000);
},
getUsage: (id, callback) => {
setTimeout(() => {
switch (id) {
case 'DDW2AU':
callback(null, [2.1, 3.2, 6.2, 5.6, 1.2]);
break;
case 'DV50PD':
callback(null, [6.3, 5.2, 1.1, 5.7, 2.6]);
break;
case 'WCGL6F':
callback(null, [6.2, 2.6, 5.8, 6.2, 9.5]);
break;
default:
callback(new Error ('not found'))
}
}, 500);
}
};
function calculate(id, callback) {
clientApi.getUsers((error, result) => {
if (error) { return callback(error); }
const currentUser = result.users.find((user) => user.id === id);
if (!currentUser) { return callback(new Error('user not found')); }
clientApi.getUsage(id, (error, usage) => {
if (error) { return callback(error); }
clientApi.getRate(id, (error, rate) => {
if (error) { return callback(error); }
callback(null, {
id,
address: currentUser.address,
due: (rate * usage.reduce((prev, curr) => curr + prev)).toFixed(2)
});
});
});
});
}
calculate('DDW2AU', (error, result) => {
console.log(error, result);
});