-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy path1-adapter.js
More file actions
32 lines (27 loc) · 809 Bytes
/
1-adapter.js
File metadata and controls
32 lines (27 loc) · 809 Bytes
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
'use strict';
// Task: create Promise-returning adapter function `totalAsync`
// Do not change function `total` contract, just call `total` from
// `totalAsync` and convert contracts
// Should not be changed
const total = (items, callback) => {
let result = 0;
for (const item of items) {
if (item.price < 0) {
callback(new Error('Negative price is not allowed'));
return;
}
result += item.price;
}
callback(null, result);
};
// const totalAsync = (items) => new Promise...
const electronics = [
{ name: 'Laptop', price: 1500 },
{ name: 'Keyboard', price: 100 },
{ name: 'HDMI cable', price: 10 },
];
// Also rewrite call, use .then instead of callback
total(electronics, (error, money) => {
if (error) console.error({ error });
else console.log({ money });
});