Skip to content

Commit c514225

Browse files
feat: add generator functions
1 parent 274929a commit c514225

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
INFO: What is a Generator ?
3+
A generator is a special function that can pause and resume its execution using the yield keyword. It automatically creates an iterator.
4+
*/
5+
6+
function* generatorName() {}
7+
/*
8+
- Its returns an iterator
9+
- yield is used to pause and return values one at a time
10+
- Your control flow manually with .next()
11+
*/
12+
13+
// Example
14+
function* nameGenerator() {
15+
yield "rafay";
16+
yield "ali";
17+
yield "john";
18+
}
19+
20+
const gen = nameGenerator();
21+
22+
console.log(gen.next()); // {value: "rafay", done: false}
23+
console.log(gen.next()); // {value: "ali", done: false}
24+
console.log(gen.next()); // {value: "john", done: false}
25+
console.log(gen.next()); // {value: "undefined", done true}

0 commit comments

Comments
 (0)