Generator Function Learning Notes

Source: Internet
Author: User
Tags try catch

First, the basic concept

The generator function is a function that can pause execution, return a iterator object, yield is a paused identity

function* gen () {    1;      2; }

The generator function has a *, with a yield statement inside it.

function* gen () {    1;     2;     return ' 1 '= Gen ();//Call the generator function to return the iterator object Console.log (G.next ())//{"value": 1, "Done": false} Console.log ( G.next ())//{"Value": 2, "Done": false}

Console.log (G.next ())// {"Value": "1", "Done": true}
Console.log (G.next ())// {"Value": Undefined, "Done": true}

If there is no yield command internally, there is no difference between the generator function and the normal function, just pausing execution.

Ii. for of

The generator function returns a Walker object that can be traversed with a for

function* gen () {    1;     2;     return ' 1 '}for(Let I of Gen ()) {  console.log (i)}
1
2
Third, next return throw

The iterator object has these three methods, but the general built-in default Symbol.iterator returns the object without the return throw method.

The 1.next method can pass data to the generator function. The yield command itself has no return value, and the next method parameter can be used as the yield assignment.

function* gen () {    1;     = yield 2;    Console.log (a)//10    return= Gen (); Console.log (G.next ())//{"value": 1, "Done": false }console.log (G.next ())//{"value": 2, "Done": false} 10console.log (G.next))//{"value": Ten, "Done": false}

The 2.Generator function returns the value of the iterator object, as well as a return method that returns the given value and the end traversal of the generator function.

function* gen () {  1;   2;   3;} var g = Gen (); G.next ()        //  {value:1, done:false}G.return // {value: ' foo ', done:true} traversal end G.next ()        //  {value:undefined, done:true}

The iterator object returned by the 3.Generator function has a throw method,

A. You can throw an error outside the function body and then capture it in the generator function body.

B. Internal throw error, while internal no try catch capture, external can capture

C.generator function inside throws an error caught, does not prevent code from running

 var  g = function  * () { try   {yield;  catch   (E) {console.log ( ' internal capture ') Span style= "color: #000000;"    >, E); }} 
var i = g (); I.next (); try {I. throw (' A ' throw (' B ' catch (E) {console.log ( ' external capture ') Span style= "color: #000000;" >, E);}
//Internal capture a
//external capture B

The first error is captured by a statement in the body of the generator function, catch at which point the generator function will not continue to execute, even if the execution is complete. The second time the Walker object i throws an error, because the statement inside the generator function catch has been executed, no longer captures the error, so this error is thrown out of the generator function body, captured by the statement outside the function body catch .

Notice the difference between the throw of the Walker object and the global throw, which can only be caught by a catch outside the body of the function.

Iv. yield*

Yield* is followed by a Walker object that can be used to invoke another generator function within the generator function yield* equivalent to using the for

V. Generator synchronous representation for asynchronous

Because the generator function can be paused, and yield is synchronous, all subsequent yields must not run until the previous end.

Vi. Generator and co-process

Co-city: Can exchange executive power, to achieve "pseudo-parallel" state, in fact, at the same time there is only one function in the run

The generator function is an implementation of the ECMAScript 6 for the coprocessor, but it is not fully implemented. The generator function is called a "semi-association" (semi-coroutine), which means that only the caller of the generator function can return the execution of the program to the generator function. In the case of a fully executed co-process, any function can allow the paused coprocessor to continue execution.

Vii. the generator

The generator function always returns a walker, ES6 specifies that the walker is an instance of the generator function, and also inherits the prototype method on the object of the generator function.

function* g () {  this. A = one=//  undefined 

The generator function returns always the iterator object, not the this object. All obj cannot get the A attribute

Generator Function Learning Notes

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.