Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ff1d91b

Browse files
Add examples
1 parent 3d68334 commit ff1d91b

File tree

5 files changed

+157
-0
lines changed

5 files changed

+157
-0
lines changed

‎JavaScript/1-iterator.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict';
2+
3+
const asyncIterator = {
4+
counter: 0,
5+
async next() {
6+
return {
7+
value: this.counter++, // current value
8+
done: this.counter > 3 // boolean
9+
};
10+
}
11+
};
12+
13+
const step1 = asyncIterator.next();
14+
const step2 = asyncIterator.next();
15+
const step3 = asyncIterator.next();
16+
const step4 = asyncIterator.next();
17+
console.log({ step1, step2, step3, step4 });

‎JavaScript/2-iterable.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'use strict';
2+
3+
const iterable = {
4+
[Symbol.asyncIterator]() {
5+
let i = 0;
6+
const iterator = {
7+
async next() {
8+
return {
9+
value: i++,
10+
done: i > 3
11+
};
12+
}
13+
};
14+
return iterator;
15+
}
16+
};
17+
18+
// Usage
19+
20+
const iterator = iterable[Symbol.asyncIterator]();
21+
const step1 = iterator.next();
22+
const step2 = iterator.next();
23+
const step3 = iterator.next();
24+
const step4 = iterator.next();
25+
console.log({ step1, step2, step3, step4 });
26+
27+
(async () => {
28+
for await (const step of iterable) {
29+
console.log({ step });
30+
}
31+
})();
32+
33+
(async () => {
34+
// console.log({ steps: [...iterable] });
35+
})();

‎JavaScript/3-class.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'use strict';
2+
3+
class Counter {
4+
constructor(begin, end, step = 1) {
5+
this.begin = begin;
6+
this.end = end;
7+
this.step = step;
8+
}
9+
[Symbol.asyncIterator]() {
10+
const end = this.end;
11+
let i = this.begin;
12+
const iterator = {
13+
async next() {
14+
return {
15+
value: i++,
16+
done: i > end
17+
};
18+
}
19+
};
20+
return iterator;
21+
}
22+
}
23+
24+
// Usage
25+
26+
const iterable = new Counter(0, 3);
27+
28+
const iterator = iterable[Symbol.asyncIterator]();
29+
const step1 = iterator.next();
30+
const step2 = iterator.next();
31+
const step3 = iterator.next();
32+
const step4 = iterator.next();
33+
console.log({ step1, step2, step3, step4 });
34+
35+
(async () => {
36+
for await (const step of iterable) {
37+
console.log({ step });
38+
}
39+
})();
40+
41+
(async () => {
42+
// console.log({ steps: [...iterable] });
43+
})();

‎JavaScript/4-generator.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'use strict';
2+
3+
const gen = async function* () {
4+
let i = 0;
5+
while (true) {
6+
if (i >= 3) return;
7+
yield i++;
8+
}
9+
};
10+
11+
{
12+
const iterable = gen();
13+
const iterator = iterable[Symbol.asyncIterator]();
14+
const step1 = iterator.next();
15+
const step2 = iterator.next();
16+
const step3 = iterator.next();
17+
const step4 = iterator.next();
18+
Promise.all([step1, step2, step3, step4]).then(steps => {
19+
console.log({ steps });
20+
});
21+
}
22+
23+
(async () => {
24+
const iterable = gen();
25+
for await (const step of iterable) {
26+
console.log({ step });
27+
}
28+
})();
29+
30+
(async () => {
31+
const iterable = gen();
32+
//console.log({ steps: [await ...iterable] });
33+
})();

‎JavaScript/5-yield.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'use strict';
2+
3+
const gen = async function* () {
4+
yield* [0, 1, 2];
5+
};
6+
7+
{
8+
const iterable = gen();
9+
const iterator = iterable[Symbol.asyncIterator]();
10+
const step1 = iterator.next();
11+
const step2 = iterator.next();
12+
const step3 = iterator.next();
13+
const step4 = iterator.next();
14+
Promise.all([step1, step2, step3, step4]).then(steps => {
15+
console.log({ steps });
16+
});
17+
}
18+
19+
(async () => {
20+
const iterable = gen();
21+
for await (const step of iterable) {
22+
console.log({ step });
23+
}
24+
})();
25+
26+
(async () => {
27+
const iterable = gen();
28+
//console.log({ steps: [await ...iterable] });
29+
})();

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /