Skip to content

Commit dd0d4bd

Browse files
committed
started docing generators
1 parent 3ca659c commit dd0d4bd

File tree

3 files changed

+68
-1
lines changed

3 files changed

+68
-1
lines changed

SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
* [Template Strings](docs/template-strings.md)
1313
* [Spread Operator](docs/spread-operator.md)
1414
* [Async Await](docs/async-await.md)
15+
* [Generators](docs/generators.md)
1516
* [TypeScript's Type System](docs/types/type-system.md)
1617
* [Ambient Declarations](docs/types/ambient-declarations.md)
1718
* [Functions](docs/types/type-compatability.md)

docs/staging/async-await.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
# Async Await
1+
### Async - Await
2+
3+
{% include "footer.md" %}

docs/staging/generators.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
### Generators
2+
Also called `function *`, generators allow you to create functions whose execution can be paused and then later resumed maintaining the state between pause-resume transitions. The value returned from a generator is called an `iterator` and can be used to control this `pause-resume` transition.
3+
4+
Here is a simple example of a generator function that generates an *infinite* list of integers.
5+
6+
```ts
7+
function* integers(){
8+
var current = 0;
9+
while(true){
10+
yield current++;
11+
}
12+
}
13+
```
14+
15+
The `yield` contextual keyword is used to return control from a generator (effectively pausing function execution) along with an optional value (here `current`). You can get access to this value using the `iterator`'s `.next()` member function, this is shown below:
16+
17+
```ts
18+
function* integers(){
19+
var current = 0;
20+
while(true){
21+
yield current++;
22+
}
23+
}
24+
var iterator = integers();
25+
console.log(iterator.next()); // 0
26+
console.log(iterator.next()); // 1
27+
console.log(iterator.next()); // 2
28+
// so on till infinity....
29+
```
30+
31+
Now that you have seen `function*`, `yield` and `.next()` we can dig deeper.
32+
33+
#### Catching Errors
34+
Any errors thrown (intentially using `throw` or unintentionally due to error) from the generator can be caught using `try/catch` just like normal function executions. This is demonstrated below:
35+
36+
```ts
37+
function* integers(){
38+
var current = 0;
39+
while(true){
40+
if (current === 3)
41+
throw new Error('3 is the magic number');
42+
else
43+
yield current++;
44+
}
45+
}
46+
var iterator = integers();
47+
console.log(iterator.next()); // 0
48+
console.log(iterator.next()); // 1
49+
console.log(iterator.next()); // 2
50+
try{
51+
console.log(iterator.next()); // Will throw an error
52+
}
53+
catch(ex){
54+
console.log(ex.message); // 3 is the magic number
55+
}
56+
```
57+
58+
#### Controlling function execution externally
59+
The iterator returned from the generator function can be used to control the state *inside* the generator function as well.
60+
61+
// TODO: example
62+
63+
64+
{% include "footer.md" %}

0 commit comments

Comments
 (0)