Skip to content

Commit 4b116d9

Browse files
committed
Examples for yield and return
1 parent c1e09e9 commit 4b116d9

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

JavaScript/b-yield.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
3+
function* counter(begin, end, delta) {
4+
let value = begin;
5+
while (end > value) {
6+
value += delta;
7+
yield value;
8+
}
9+
}
10+
11+
const c = counter(0, 30, 12);
12+
const val1 = c.next();
13+
const val2 = c.next();
14+
const val3 = c.next();
15+
const val4 = c.next();
16+
console.log({ c, val1, val2, val3, val4 });

JavaScript/c-return.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict';
2+
3+
function* ids(...args) {
4+
let i = 0;
5+
while (args.length > i) {
6+
const id = args[i++];
7+
if (id === undefined) return -1;
8+
yield id;
9+
}
10+
}
11+
12+
const id = ids(1011, 1078, 1292, 1731, undefined, 1501, 1550);
13+
let val;
14+
do {
15+
val = id.next();
16+
console.log({ val });
17+
} while (!val.done);

0 commit comments

Comments
 (0)