Skip to content

Commit 9787740

Browse files
committed
Diving Deeper With ES6 Generators
1 parent 4878c0f commit 9787740

File tree

4 files changed

+102
-0
lines changed

4 files changed

+102
-0
lines changed

src/2-Diving-Deeper/ex1.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Error Handling
3+
* One can throw an error in a generator from the outside with it.throw().
4+
* Since all the code inside of a generator is synchronous,
5+
* we can use the standard try/catch method of handling errors!
6+
*/
7+
const { log } = require('../log');
8+
9+
// Code without any error handling
10+
function* createHello() {
11+
const word = yield;
12+
log(`Hello ${word}`);
13+
}
14+
15+
const hello = createHello();
16+
hello.next();
17+
18+
// Lots of business logic might be going on between 1st next() and last next()
19+
// and our whole app could crash if an error occurs here
20+
// and it is not handled properly
21+
// throw new Error('Unhandled Error');
22+
23+
hello.next('World!');

src/2-Diving-Deeper/ex2.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Error Handling
3+
* One can throw an error in a generator from the outside with it.throw().
4+
* Since all the code inside of a generator is synchronous,
5+
* we can use the standard try/catch method of handling errors!
6+
*/
7+
const { log } = require('../log');
8+
9+
// Code without any error handling
10+
function* createHello() {
11+
try {
12+
const word = yield;
13+
log(`Hello ${word}`);
14+
} catch (err) {
15+
log(`Error: ${err}`);
16+
}
17+
}
18+
19+
const hello = createHello();
20+
hello.next();
21+
22+
// Throw an error in a generator from the outside
23+
// hello.throw('Something went Wrong!');
24+
try {
25+
throw new Error('Something went wrong!');
26+
} catch (err) {
27+
hello.throw(err.message);
28+
}
29+
30+
hello.next('World!');

src/2-Diving-Deeper/ex3.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Delegating Generators
3+
* We can execute generators from generators,
4+
* and delegate the iteration control with the yield* keyword.
5+
*/
6+
const { log, delaySync } = require('../log');
7+
8+
function* create3To4Counter() {
9+
yield 3;
10+
yield 4;
11+
}
12+
13+
function* createCounter() {
14+
yield 1;
15+
yield 2;
16+
yield* create3To4Counter();
17+
yield 5;
18+
}
19+
20+
for (const value of createCounter()) {
21+
log(value);
22+
delaySync(600);
23+
}

src/2-Diving-Deeper/ex4.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Delegating Generators
3+
* Communication between two different generators is also possible
4+
*/
5+
const { log, delaySync } = require('../log');
6+
7+
function* create3To4Counter() {
8+
delaySync(800);
9+
yield 3;
10+
11+
// Return a value from delegated generator
12+
return 4;
13+
}
14+
15+
function* createCounter() {
16+
yield 1;
17+
yield 2;
18+
const four = yield* create3To4Counter();
19+
log(four);
20+
yield 5;
21+
}
22+
23+
for (const value of createCounter()) {
24+
log(value);
25+
delaySync(400);
26+
}

0 commit comments

Comments
 (0)