1function* idMaker() {
2 var index = 0;
3 while (true)
4 yield index++;
5}
6
7var gen = idMaker();
8
9console.log(gen.next().value); // 0
10console.log(gen.next().value); // 1
11console.log(gen.next().value); // 2
12console.log(gen.next().value); // 3
13// ...
1# A generator function that yields 1 for first time,
2# 2 second time and 3 third time
3def simpleGeneratorFun():
4 yield 1
5 yield 2
6 yield 3
7
8# Driver code to check above generator function
9for value in simpleGeneratorFun():
10 print(value)
11
1function* indexGenerator(){
2 var index = 0;
3 while(true) {
4 yield index++;
5 }
6}
7const g = indexGenerator();
8console.log(g.next().value); // => 0
9console.log(g.next().value); // => 1
1function* makeRangeIterator(start = 0, end = 100, step = 1) {
2 let iterationCount = 0;
3 for (let i = start; i < end; i += step) {
4 iterationCount++;
5 yield i;
6 }
7 return iterationCount;
8}