File tree Expand file tree Collapse file tree 1 file changed +25
-0
lines changed
part5 (Functions)/AdvanceFunctions Expand file tree Collapse file tree 1 file changed +25
-0
lines changed Original file line number Diff line number Diff line change
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}
You can’t perform that action at this time.
0 commit comments