Here's a ES6 generator:
function *Gen() {
var input1 = yield 'output1'
}
var gen = Gen()
gen.next('input1').value // return 'output1'
gen called 1st time, return output1
but the variable input1
doesn't equal 'input1'
which passed in , the value of input
actually is the 'input2', 2nd time next('input2')
called
My question is how to access the input1
which is the first time next called, something like:
function *Gen() {
var input 1 = param1
var input2 = yield 'output1'
}
The ES6 generator is a new type of function that can be paused in the middle or many times in the middle and resumed later. In the standard function, control remains with the called function until it returns, but the generator function in ES6 allows the caller function to control the execution of a called function.
Generators are a special class of functions that simplify the task of writing iterators. A generator is a function that produces a sequence of results instead of a single value, i.e you generate a series of values.
When called, generator functions do not initially execute their code. Instead, they return a special type of iterator, called a Generator. When a value is consumed by calling the generator's next method, the Generator function executes until it encounters the yield keyword.
The function* declaration ( function keyword followed by an asterisk) defines a generator function, which returns a Generator object.
When you obtain the generator you don't have yield
s you can push value to (you are at the beginning of the generator function). You need to trigger the generator and reach the first yield
by calling gen.next()
without any arguments. At this point you have an yield
at your disposal and you can push your value via gen.next('input1')
effectively replacing the expression yield 'output1'
with the value you passed to next
- 'input1'. Then you need another yield
or a return
to provide your custom value to the consumer of the generator. Here is the code:
function *Gen() {
var input1 = yield 'output1'
return input1
}
var gen = Gen()
gen.next();
gen.next('input1').value // return 'input1'
If you want a parametrised generator function you can use a higher order function that returns generator:
function myGenerator(startFrom) {
return (function *() {
var i = startFrom;
while (true) yield i++;
})();
}
var gen = myGenerator(5);
console.log(gen.next().value) // 5
console.log(gen.next().value) // 6
console.log(gen.next().value) // 7
Higher order generators can be of use too:
function getGenerator(baseStartFrom, expStartFrom) {
return (function *() {
var a = baseStartFrom;
while (true) {
yield (function *() {
var i = expStartFrom;
while (true) yield Math.pow(a, i++);
})();
a++;
}
})();
}
var gen = getGenerator(2, 3);
var gen2 = gen.next().value; // generator yields powers of 2
console.log(gen2.next().value); // 8
console.log(gen2.next().value); // 16
console.log(gen2.next().value); // 32
var gen3 = gen.next().value; // generator yields powers of 3
console.log(gen3.next().value); // 27
console.log(gen3.next().value); // 81
console.log(gen3.next().value); // 243
The sample is most likely useless but the same approach can be used, for example, to generate random number generators.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With