🟦

Generator*

notion image
yeald call run function, next() will response, when yeald be called again will back from the last line called from the last yeal
A generator is a special type of function in JavaScript that can be paused and resumed multiple times. The yield keyword is used to pause the execution of the generator and return a value. The generator can be resumed by calling the next() method on it. Each time next() is called, the generator will continue from the last line of code that was executed before it was paused.
Here's a simple example to demonstrate the use of generators:
function* numbers() { yield 1; yield 2; yield 3; } const generator = numbers(); console.log(generator.next().value); // 1 console.log(generator.next().value); // 2 console.log(generator.next().value); // 3
In the example above, the numbers() function is a generator that returns the values 1, 2, and 3. The yield keyword is used to pause the generator and return the value. The generator can be resumed by calling the next() method on it.
Redux library that use the generator principle as his core: