Skip to main content

ES6 generator functions

 Generators are a new function control and usage scheme in ES6 that gives you more control over how functions continue, pause, and more. We usually write a lot of functions, and the conditions for these functions to terminate are usually return values or exceptions. Generator functions are also functions, but with a few differences from regular functions. First, generator functions should add the symbol "*" after the function. The generator function can then use the yield keyword to control the execution. Finally, the return value of a generator function is a generator. A generator is actually a special kind of iterator.

Declaration method:

  function* function name (parameter1, parameter...n).

Generator function call:

  • A generator's iterator object is returned when the generator object is called.
  • The iterator has a next() method that returns an object containing value and done, where value is the return value and done is the execution (true or false).
  • Each time the next() method is called, execution is suspended at the position of the yield expression.
  • value indicates the return value of the yield expression, and done indicates whether the last value of the generator has been generated.
  • If you pass a parameter to the next() method, the value of the parameter will replace the return value of the yield expression. If you call return in the generator, the generator will end early.

Example:

Define a generator function that calls the next() method each time.

function * fun(){
    console.log("before");
    yield *fun2();
    console.log("11111");
    yield '1111---yield';
    console.log("2222");
    yield '2222---yield'
}
function * fun2(){
    console.log("fun2----111");
    yield "666666";
    console.log("fun2-2222");
}
let f=fun();
console.log(Object.prototype.toLocaleString.call(f));
f.next();
f.next();
f.next();
// f.next();
// f.next();
Iterate over the output:
for(let val of f){
    console.log(val.value,val.done);
}

Note: calling return in a generator, the generator will end early:

The value passed in the first call to next() is not used, just to start executing the generator function. The value passed in the second call to next() will be used as the return result of the first yield statement, which can be received by yourself in a variable, and then printed out for viewing.

Comments

Popular posts from this blog

A simple understanding of ES6 iterators

 What is an iterator?An iterator is an interface that provides a unified access mechanism for various data structures. Any data structure can complete the traversal operation as long as the iterator interface is deployed.ES6 created a new traversal command for...of loop, which natively has a data structure with the iterator interface (which can be traversed with for...of). Contains Array, Arguments, Set, Map, String, TypedArray, NodeList. Of course, you can also implement this interface manually, which is convenient for practical use.