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 yield in es6

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

Share

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

This article mainly introduces the relevant knowledge of "how to use yield in es6". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to use yield in es6" can help you solve the problem.

In es6, yield is used to pause and resume the operation of an ergodic function; yield is a command keyword that returns a state value that tells the program its current running state, and the function is to pause a resume with the syntax of "[rv] = yield [expression];"

This tutorial operating environment: windows10 system, ECMAScript version 6. 0, Dell G3 computer.

What is the use of yield in es6

The yield keyword is used to pause and resume a traversal function.

Yield is a keyword, and its function is "command". Unlike var, it is not used to declare, but like return, it is used to tell the program a certain state, return tells the program what value to return (which also means it is over, and the value will not be returned until the end), while yield tells the program the current state value, and you run here to pause.

Yield is an imperative keyword, so its usage is:

[rv] = yield [expression]

Rv is optional, not to say that it returns an array. The expression after yield is also optional. The return value of yield is a status value. Yield can also be thought of as an operator in terms of return value, but because its function is to pause and resume, strictly speaking, it cannot be called an operator, which is used for operations, while yield is used for "commands."

Think of yield as a variable. In a string, you can use the following:

Var log = function * () {console.log (`next: ${yeild} `)} () .next (); / / error: yeild undefinedlog.next ('hello worldview')

To sum up yield, in fact:

Can only be used inside the Generator function

Run .next () and pause when you encounter a yield command

The return value of .next () indicates a state {value,done}

Run .next () again to resume running from the yield [expression] encountered earlier (after)

When .next () passes a parameter, the entire yield [expression] is replaced with the parameter passed in.

This is the end of the content about "how to use yield in es6". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

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