-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiterator.js
More file actions
31 lines (24 loc) · 812 Bytes
/
iterator.js
File metadata and controls
31 lines (24 loc) · 812 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
let myIterable = {};
myIterable[Symbol.iterator] = iter;
function* iter() {
yield 1;
yield 2;
yield 3;
};
console.log([...myIterable]); // [1, 2, 3] // spread operator
function display(a,b,c,d) { console.log(a,b,c,d) }
display(...myIterable); // 1 2 3 undefined
let objClone = { ...myIterable };
console.log(objClone); // { [Symbol(Symbol.iterator)]: [GeneratorFunction] }
const i = iter();
console.log(i.next().value,i.next().value,i.next().value,i.next().value); // 1 2 3 undefined
let a,b,c,d
[a,b,c,d]=myIterable
console.log(a,b,c,d); // 1 2 3 undefined
async function test() {
for (let value of myIterable)
console.log(value); // 1, 2, 3
for await (const x of myIterable) // this way the yields can also return promises
console.log(x); // 1 2 3
}
test();