Skip to content

Commit 4878c0f

Browse files
committed
The Basics Of ES6 Generators
1 parent 4a343dd commit 4878c0f

File tree

10 files changed

+181
-2
lines changed

10 files changed

+181
-2
lines changed

.editorconfig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
* text=auto
2+
*.js text eol=lf

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
node_modules
2+
.DS_Store
3+
*.log
4+
.idea
5+
.vscode/
6+
.eslintrc
7+
.eslintignore

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,10 @@
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

package.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
}

src/1-Basics/ex1.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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"

src/1-Basics/ex2.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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

src/1-Basics/ex3.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
}

src/1-Basics/ex4.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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');

src/log.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
};

0 commit comments

Comments
 (0)