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

Use of js array filter() method

 Arrays are frequently used in development, and processing data in arrays is one of the more common and important operations. Therefore, processing data in arrays during development is an important skill. Every developer Everyone should master the operations of arrays, especially for junior developers who have just entered the industry, so be sure to master the relevant skills. This article mainly shares some operations to filter the data in the array, about the use of the filter() method.

ES6 arrow functions

 In ES6, in addition to the new features of let and const, arrow functions are the most frequently used new features. But this feature is not unique to ES6. Arrow functions, as the name suggests, are functions defined using arrows (=>) and belong to a class of anonymous functions. It allows us to write smaller function syntaxes. The code of arrow functions is simpler and more flexible to write.