File tree Expand file tree Collapse file tree 10 files changed +181
-2
lines changed Expand file tree Collapse file tree 10 files changed +181
-2
lines changed Original file line number Diff line number Diff line change
1
+ root = true
2
+
3
+ [* ]
4
+ indent_style = tab
5
+ end_of_line = lf
6
+ charset = utf-8
7
+ trim_trailing_whitespace = true
8
+ insert_final_newline = true
Original file line number Diff line number Diff line change
1
+ * text =auto
2
+ * .js text eol =lf
Original file line number Diff line number Diff line change
1
+ node_modules
2
+ .DS_Store
3
+ * .log
4
+ .idea
5
+ .vscode /
6
+ .eslintrc
7
+ .eslintignore
Original file line number Diff line number Diff line change 1
- # learn-generators
2
- Learning about ES6 Generators: Complete Series
1
+ # Learn ES6 Generators
2
+
3
+ This repo is used to learn and explore more about ES6 Generators.
4
+
5
+ Topics covered:
6
+
7
+ - [x] The Basics Of ES6 Generators
8
+ - [x] Diving Deeper With ES6 Generators
9
+ - [x] Going Async With ES6 Generators
10
+ - [x] Getting Concurrent With ES6 Generators
Original file line number Diff line number Diff line change
1
+ {
2
+ "name" : " learn-generators" ,
3
+ "private" : true ,
4
+ "version" : " 1.0.0" ,
5
+ "description" : " This repo is used to learn and explore more about ES6 Generators." ,
6
+ "main" : " index.js" ,
7
+ "scripts" : {
8
+ "test" : " echo \" Error: no test specified\" && exit 1"
9
+ },
10
+ "repository" : {
11
+ "type" : " git" ,
12
+ "url" : " git+https://github.com/palashmon/learn-generators.git"
13
+ },
14
+ "keywords" : [],
15
+ "author" : " Palash Mondal" ,
16
+ "license" : " MIT" ,
17
+ "bugs" : {
18
+ "url" : " https://github.com/palashmon/learn-generators/issues"
19
+ },
20
+ "homepage" : " https://github.com/palashmon/learn-generators#readme" ,
21
+ "dependencies" : {
22
+ "moment" : " ^2.24.0"
23
+ }
24
+ }
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Run To Completion
3
+ * Functions: once the function starts running, it will always
4
+ * run to completion before any other JS code can run.
5
+ */
6
+ const { log, delaySync } = require ( '../log' ) ;
7
+
8
+ setTimeout ( ( ) => {
9
+ log ( '\nHello World' ) ;
10
+ } , 1 ) ;
11
+
12
+ function main ( ) {
13
+ for ( let i = 0 ; i <= 10 ; i += 1 ) {
14
+ log ( i ) ;
15
+ delaySync ( 500 ) ;
16
+ }
17
+ }
18
+
19
+ main ( ) ;
20
+ // 0..10
21
+ // "Hello World"
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Syntax of these new and exciting generator functions.
3
+ * Generators are a special class of functions that simplify the task of writing iterators.
4
+ * A generator is a function that produces a sequence of results instead of a single value, i.e you generate a series of values.
5
+ */
6
+ const { log, delaySync } = require ( '../log' ) ;
7
+
8
+ // Creating a Generator
9
+ function * generatorFunction ( ) {
10
+ log ( '\nThis will be executed first.' ) ;
11
+ yield 'Hello, ' ;
12
+
13
+ delaySync ( 2000 , ! 0 ) ;
14
+
15
+ log ( '\nI will be printed after the pause' ) ;
16
+ yield 'World!' ;
17
+ }
18
+
19
+ const generatorObject = generatorFunction ( ) ;
20
+ log ( generatorObject . next ( ) . value ) ; // next = { value:'Hello,', done:false }
21
+ // This will be executed first.
22
+ // Hello,
23
+ log ( generatorObject . next ( ) . value , '\n' ) ; // next = { value:'World!', done:false }
24
+ // I'm 2000 ms sync delay...
25
+ // I will be printed after the pause
26
+ // World!
27
+ log ( generatorObject . next ( ) . value ) ; // next = { value:undefined, done:true }
28
+ // undefined
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Iterators and generators
3
+ *
4
+ * Since generators are also iterables, they can be used to implement iterables
5
+ * without the extra boilerplate code. Let’s see a simple example.
6
+ */
7
+ const { log, delaySync } = require ( '../log' ) ;
8
+
9
+ // Generator function
10
+ function * iterableObj ( ) {
11
+ yield 'This' ;
12
+ delaySync ( 600 ) ;
13
+ yield 'is' ;
14
+ delaySync ( 600 ) ;
15
+ yield 'iterable.' ;
16
+ }
17
+
18
+ // Iterators
19
+ for ( const val of iterableObj ( ) ) {
20
+ log ( val , '\n' ) ;
21
+ }
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Iterators and generators
3
+ *
4
+ * Processing each of the items in a collection is a very common operation.
5
+ * JavaScript provides a number of ways of iterating over a collection,
6
+ * from simple for loops to map() and filter().
7
+ * Iterators and Generators bring the concept of iteration directly into the core language
8
+ * and provide a mechanism for customizing the behavior of for...of loops.
9
+ */
10
+ const { log, delaySync } = require ( '../log' ) ;
11
+
12
+ // Generator functions
13
+ function * makeRangeIterator ( start = 0 , end = 100 , step = 1 ) {
14
+ for ( let i = start ; i < end ; i += step ) {
15
+ yield i ;
16
+ }
17
+ }
18
+
19
+ // Iterators
20
+ const it = makeRangeIterator ( 1 , 10 , 2 ) ;
21
+
22
+ // We can use for...of
23
+ /*
24
+ for (const val of it) {
25
+ log(val);
26
+ delaySync(600);
27
+ }
28
+ */
29
+
30
+ // Or, while loop also like this
31
+ let result = it . next ( ) ;
32
+ while ( ! result . done ) {
33
+ log ( result . value ) ; // 1 3 5 7 9
34
+ result = it . next ( ) ;
35
+ delaySync ( 600 ) ;
36
+ }
37
+
38
+ log ( '\nIterated over the sequence' ) ;
Original file line number Diff line number Diff line change
1
+ const moment = require ( 'moment' ) ;
2
+
3
+ const log = function ( ...args ) {
4
+ const time = [ moment ( ) . format ( 'HH:mm:ss:' ) ] ;
5
+ Array . prototype . push . apply ( time , args ) ;
6
+ console . log . apply ( null , args ) ;
7
+ } ;
8
+
9
+ // Not a generator, just function
10
+ const delaySync = function ( time , displayLog = false ) {
11
+ if ( displayLog ) log ( "\nI'm" , time , 'ms sync delay...' ) ;
12
+ const endTime = new Date ( ) . valueOf ( ) + time ;
13
+ let now = new Date ( ) ;
14
+ while ( now . valueOf ( ) < endTime ) {
15
+ now = new Date ( ) ;
16
+ }
17
+ } ;
18
+
19
+ module . exports = {
20
+ log,
21
+ delaySync
22
+ } ;
You can’t perform that action at this time.
0 commit comments