Nodejs Study notes Four

Source: Internet
Author: User

Generator Introduction Basic Concepts

The generator function has several angles of understanding. Syntactically, it can be understood first that the generator function is a state machine that encapsulates multiple internal states.

Executing the Generator function returns a Walker object, that is, the generator function, in addition to the state machine, is a Walker object generation function. Returns the iterator object that iterates through each state within the generator function.

Formally, the generator function is a normal function, but there are two characteristics. One is that there is an asterisk between the function command and the letter name, and the other is that the yield statement is used internally to define different internal states (the yield statement means "output" in English).

function* Helloworldgenerator () {  yield ' hello ';  Yield ' world ';  return ' ending ';} var hw = Helloworldgenerator (); Console.log (Hw.next ()); Console.log (Hw.next ()); Console.log (Hw.next ()); Console.log ( Hw.next ()); run result:>   > {value: ' Hello ', done:false}> {value: ' World ', Done:false}> {value: ' Ending ', D One:true}> {value:undefined, done:true}

  

The above code defines a generator function helloworldgenerator, which has two yield statements "hello" and "world", that is, the function has three states: Hello,world and return statements (end execution).

The generator function is then called in the same way as a normal function, with a pair of parentheses appended to the function name. The difference is that after calling the generator function, the function does not execute, nor does it return the result of the function run, but rather a pointer to the internal state of the object, the Walker object (Iterator objects).

Next method

Invokes the next method of the Walker object, allowing the pointer to move to the next state. That is, each time the next method is called, the internal pointer executes from the function's head or the last stop, until the next yield statement (or RETURN statement) is encountered. In other words, the generator function is executed in segments, and the yield statement is the token that suspends execution, and the next method can resume execution.

    1. HW. Next()
    2. {value: ' Hello ', done:false}
    3. HW. Next()
    4. {value: ' World ', Done:false}
    5. HW. Next()
    6. {value: ' Ending ', done:true}
    7. HW. Next()
    8. {value:undefined, done:true}

The above code calls the next method altogether four times.

The first call, the generator function begins execution until the first yield statement is encountered. The next method returns an object whose Value property is the value of the current yield statement Hello,done property, False, indicating that the traversal has not ended.

The second call, where the generator function stops from the last yield statement, executes to the next yield statement. The Value property of the object returned by the next method is the World,done value of the current yield statement property, False, indicating that the traversal has not ended.

For the third invocation, the generator function executes to the return statement from where the last yield statement was stopped (and executes to the end of the function if there is no return statement). The Value property of the object returned by the next method is the value of the expression immediately following the return statement (if there is no return statement, the Value property is undefined) and the Done property evaluates to true, indicating that the traversal has ended.

The fourth call, at which time the generator function has finished running, the next method returns the object's Value property to the Undefined,done property of True. The next method is called later, and the value is returned.

To summarize, call the generator function and return a Walker object that represents an internal pointer to the generator function. Later, each time the next method of the Walker object is called, an object with value and done two properties is returned. The Value property represents the current internal state, which is the value of the expression after the yield statement; The Done property is a Boolean value that indicates whether the traversal ends.

Nodejs Study notes Four

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.