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

vue routing global guard beforeEach and afterEach

 Global routing front guard (beforeEach) This function is used the most. Its function is to perform permission-related verification before routing jumps. This function contains three parameters: to: the object of the target route that is about to enter; from: the route that the current route is leaving; next: confirm the release. It can be used to log in and register, to determine whether there is a token before logging in, and release if it exists. , if it does not exist, it will not be released. The post routing guard (afterEach), its role is to trigger after the routing jump.

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.