-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy path3-async.js
More file actions
46 lines (41 loc) · 1.37 KB
/
3-async.js
File metadata and controls
46 lines (41 loc) · 1.37 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
'use strict';
// Task: rewrite `total` function to be async with JavaScript timers
// Use `setInterval` and `clearInterval` to check next item each 1 second
// Calculations will be executed asynchronously because of timers
// Run `total` twice (as in example below) but in parallel
// Print debug output for each calculation step (each second)
//
// Hint: example output:
// { check: { item: { name: 'Laptop', price: 1500 } } }
// { check: { item: { name: 'Laptop', price: 1500 } } }
// { check: { item: { name: 'Keyboard', price: 100 } } }
// { check: { item: { name: 'Keyboard', price: 100 } } }
// { check: { item: { name: 'HDMI cable', price: 10 } } }
// { check: { item: { name: 'HDMI cable', price: 10 } } }
// { money: 1610 }
// { money: 1610 }
const total = (items, callback) => {
let result = 0;
for (const item of items) {
console.log({ check: { item } });
if (item.price < 0) {
callback(new Error('Negative price is not allowed'));
return;
}
result += item.price;
}
callback(null, result);
};
const electronics = [
{ name: 'Laptop', price: 1500 },
{ name: 'Keyboard', price: 100 },
{ name: 'HDMI cable', price: 10 },
];
total(electronics, (error, money) => {
if (error) console.error({ error });
else console.log({ money });
});
total(electronics, (error, money) => {
if (error) console.error({ error });
else console.log({ money });
});