ES6 generator method example analysis

The example of this paper describes the ES6 generator usage. Share to you for your reference, specifically:

Syntax

What is the generator? Let’s take a look at the following code:

 Function * quips (name) {yield "Hello" + Name + "!"; Yield "I Hope you are enjoying the blog POSTS "; if (Name.StartSwith (" x ")) {yield" it's cool how your name;} yield "see you latile!";}   
You didn’t read the wrong, this is the JavaScript code. Is it not the same as JavaScript you have known. This function is called generator function. The generator function does not seem to be a bit similar and ordinary function?

Their differences are as follows:

The general function starts in a function, and the generator function begins with function *

.

There is a special key in the generator function is Yield, and the role is to pause the function. Cooperate with the next method to call the purpose of the execution function that can reach a step. .

Let’s take a look at the NEXT method: > var t = quips (“lf”); [Object Generator]> iter.next () {Value: “Hello LF!”, DONE: FALSE}> iter.next () {value: “i Hope YOu is: false}> iter.next () {value: “See you lat!”, DONE: FALSE}> iter.next () {value: undefined, done: true}

As can be seen, after each Next method, the generator function is executed to the next Yield location.
 The basic generator syntax is finished.   

The generator can be used asynchronously, I have introduced in the previous article, and I can refer to one “asynchronous programming instance based on JavaScript” before ”

But it should be noted that the generator is not used to write asynchronous, which is just a HACK means.

It is desirable to help the ECMAScript program design this article.

© Copyright Notice
THE END
Just support it if you like
like0
share
comment Grab the couch

Please log in to comment