Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use generator in es6

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the relevant knowledge of "how to use the generator in es6". In the operation process of actual cases, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!

In es6, generator is used to encapsulate asynchronous tasks, is a container of asynchronous tasks, can let the function according to the specified time execution or pause; when defining the function, there is an asterisk (*) between the function keyword and the function name, the syntax is "function *name(){.. yield..} "。

Operating environment for this tutorial: Windows 10, ECMAScript version 6.0, Dell G3 PC.

What is the use of generator in ES6?

Generator is mainly asynchronous programming, used to encapsulate asynchronous tasks, is a container of asynchronous tasks, you can let the function execute or pause according to the time we specify.

The difference between defining a Generator function and defining a normal function is:

There is an *(asterisk) between the function keyword and the function name.

Function internals Use yield to define the internal state of each function.

If there is a return statement inside the function, it is the last state inside the function.

Usage syntax:

function *name(){... yield; //add yield when you need to pause... yield;...} const p = name();p.next() //Call the function, execute until the first yield stops p.next() //Start execution from the previous yeild until the next yield

Let's look at a simple example:

//define function* sayHello() { yield 'hello'; yield 'world'; return 'ending';}//call//note that the value hw gets is a traversal object let g = sayHello();

The above example defines a Generator function called sayHello that has two yield expressions and a return expression inside. So, there are three states inside the function: hello, world, and a return statement (ending execution). Finally, call this function, get a traversal object and assign it to the variable g.

The Generator function is called exactly the same way as the ordinary function, the function name (). The difference is:

After the function is called, none of the internal code (starting with the first line) executes immediately.

A function call returns a value, which is a pointer object to the internal state of the function, essentially a traversal object that contains the internal state of the function.

"How to use generator in es6" is introduced here. Thank you for reading it. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report