This is the 18th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

Examples of TypeScript

The content of this article: generator functions.Copy the code

The generator

A generator is a function that can be stopped in mid-stream and can pick up where it left off. A function is usually stopped by return or yield. The function * syntax creates a generator function that returns an object on which the next() method can be called.

Delay iterator

Generator functions can be used to create delayed iterators.

/ / 1
function* generatorDemo() {
    // This is the first execution
    yield 'first'

    // Execute the second time
    yield 'second'
    
    // Execute the third time
    yield 'third'
}

let iterator = generatorDemo()
console.log(iterator.next().value)   // first
console.log(iterator.next().value)   // second
console.log(iterator.next().value)   // third
Copy the code

Example 1 uses the function* syntax to create a generator function that is called and assigned to the variable iterator. Generator functions are not executed when called. It simply creates a generator object. The function body uses the yield keyword to return an iterator.

The iterator variable has a next() method that executes the iterator functions returned sequentially. The first call to the next() method encounters the yield keyword, which returns the value following it, the generator generates an object {value: ‘Hello, ‘, done: false}, and the function stops running until the next() method is called again.

Calling next() after the iterator ends returns {done: true, value: undefined}, with no value (undefined by default).

/ / 2
function* getRandom() {
    while (true) {
        yield Math.floor(Math.random() * 100)}}const iterator = getRandom()
let num = 1
while (num % 9! = =0) {
    num = iterator.next().value
}

console.log(num)
Copy the code

Example 2 is a more practical example. The program stops when it generates a random number that is a multiple of 9, otherwise it keeps running.

Next () value

The next() method can pass a value to the generator with an argument, controlling the result of the last yield expression. Note: Passing in the next() method is meaningless the first time it is called.

/ / 3
function* generatorDemo() {
    // This is the first execution
    let y1 = yield 'first'

    // Execute the second time
    let y2 = yield 'second' + y1
    
    // Execute the third time
    let y3 = yield 'third' + y2
}

let iterator = generatorDemo()
console.log(iterator.next().value)     // first
console.log(iterator.next(10).value)   // second10
console.log(iterator.next(20).value)   // third20
Copy the code

The generator turns asynchronous callback code into “synchronous” code. Async await is a syntactic sugar based on generator functions, and await can wait for the asynchronous function to complete before continuing to execute the following code.

Finish this! If this article helps you at all, please give it a thumbs up.