Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I pass a parameter to ES6 generator function

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'
}
like image 262
mko Avatar asked Feb 26 '15 05:02

mko


People also ask

Is ES6 a generator function?

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.

What is unique about generator function JavaScript?

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.

What keyword does a generator use to send a generated value to the caller JavaScript?

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.

Which is the correct way to declare a generator function?

The function* declaration ( function keyword followed by an asterisk) defines a generator function, which returns a Generator object.


2 Answers

When you obtain the generator you don't have yields 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'
like image 119
gkostadinoff Avatar answered Sep 20 '22 05:09

gkostadinoff


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.

like image 22
esp Avatar answered Sep 21 '22 05:09

esp