File tree Expand file tree Collapse file tree 4 files changed +102
-0
lines changed Expand file tree Collapse file tree 4 files changed +102
-0
lines changed Original file line number Diff line number Diff line change
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!' ) ;
Original file line number Diff line number Diff line change
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!' ) ;
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments