Skip to content

Commit 1bc355b

Browse files
committed
Generators Ex
1 parent f574995 commit 1bc355b

1 file changed

Lines changed: 58 additions & 0 deletions

File tree

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
const generateNames = function* () {
2+
let names = ["johndoe", "killer", "ash", "zakir"];
3+
for (let name of names) {
4+
console.log(name);
5+
yield;
6+
}
7+
};
8+
const result = generateNames();
9+
result.next();
10+
result.next();
11+
result.next();
12+
result.next();
13+
14+
console.log("---------------------");
15+
16+
function* getEmployee() {
17+
const names = [
18+
"Amanda",
19+
"Diego",
20+
"Farrin",
21+
"James",
22+
"Kagure",
23+
"Kavita",
24+
"Orit",
25+
"Richard",
26+
];
27+
const facts = [];
28+
29+
for (const name of names) {
30+
// yield *out* each name AND store the returned data into the facts array
31+
facts.push(yield name);
32+
}
33+
34+
return facts;
35+
}
36+
const generatorIterator = getEmployee();
37+
let name = generatorIterator.next().value;
38+
39+
// pass data in *and* get the next name
40+
name = generatorIterator.next(`${name} is cool!`).value;
41+
42+
// pass data in *and* get the next name
43+
name = generatorIterator.next(`${name} is awesome!`).value;
44+
45+
// pass data in *and* get the next name
46+
name = generatorIterator.next(`${name} is stupendous!`).value;
47+
48+
// you get the idea
49+
name = generatorIterator.next(`${name} is rad!`).value;
50+
name = generatorIterator.next(`${name} is impressive!`).value;
51+
name = generatorIterator.next(`${name} is stunning!`).value;
52+
name = generatorIterator.next(`${name} is awe-inspiring!`).value;
53+
54+
// pass the last data in, generator ends and returns the array
55+
const positions = generatorIterator.next(`${name} is magnificent!`).value;
56+
57+
// displays each name with description on its own line
58+
console.log(positions.join("\n"));

0 commit comments

Comments
 (0)