-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy path3-class.js
More file actions
34 lines (30 loc) · 721 Bytes
/
3-class.js
File metadata and controls
34 lines (30 loc) · 721 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
33
34
'use strict';
// Task: rewrite to `class Iterator` implementing
// Thenable contract with private fields.
const iterate = (items) => {
let index = 0;
return {
then(fulfill /*reject*/) {
if (index < items.length) {
fulfill(items[index++]);
}
},
};
};
const electronics = [
{ name: 'Laptop', price: 1500 },
{ name: 'Keyboard', price: 100 },
{ name: 'HDMI cable', price: 10 },
];
(async () => {
const items = iterate(electronics);
// Use `new Iterator(electronics)`
const item1 = await items;
console.log(item1);
const item2 = await items;
console.log(item2);
const item3 = await items;
console.log(item3);
const item4 = await items;
console.log(item4);
})();