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

What is the difference between the box-size property content-box and border-box in the css box model?

 The box model is a very important concept in CSS layout, it includes content area, padding, border, and margin. Box models can be divided into two types: standard box models and IE box models. The box model, as the name suggests, is used to hold things, and the things it holds are the content of HTML elements. In other words, every visible HTML element is a box.

Js uses recursive way to traverse the dom tree to dynamically create element nodes

 What is a dom tree? In short, DOM is the Document Object Model, which provides a structured representation for documents and defines how to access the document structure through scripts. DOM is composed of nodes. After the HTML is loaded, the rendering engine will generate a DOM tree in memory based on the HTML document. This article uses a small case to traverse the dom tree recursively. The core of the method is to determine whether the incoming data is an array, and then traverse the root node. Note that there must be an end condition when using recursion.