Home > Article > Web Front-end > What is the usage of yield in es6
In es6, yield is used to pause and resume the running of a traverser function; yield is a command keyword, and the returned result is a status value, which can tell the current running status of the program. Its function is To pause and resume running, the syntax is "[rv] = yield [expression];"
The operating environment of this tutorial: Windows 10 system, ECMAScript version 6.0, Dell G3 computer.
The yield keyword is used to pause and resume the (running) of a traverser function.
yield is a keyword, its function is "command". Unlike var, it is not used for declaration, but like return, it is used to inform the program of a certain status. Return tells the program what value to return (it also means the end, and the value will be returned only when it ends), while yield tells the program the current state. The status value, and you pause here.
yield is a command keyword, so its usage is:
[rv] = yield [expression];
rv is optional, this does not mean that it returns an array. The expression following yield is also optional. The return value of yield is a status value. From the perspective of return value, yield can also be regarded as an operator. However, since its function is to pause and resume, it cannot be called an operator in a strict sense. Operators are used for operations, and yield is Used to "command".
Think of yield as a variable. In a string, it can be used like this:
var log = function *() { console.log(`you input: ${yeild}`) }().next(); // 这里会提示错误: yeild undefined log.next('hello world!');
To summarize yield, in fact:
Can only be used inside the Generator function
Run .next(), when encountering a yield command, pause
.next The return value of () represents a state {value,done}
Run .next() again and resume operation from (after) the yield [expression] encountered before.
When .next() passes parameters, the entire yield [expression] is replaced with the passed parameters.
[Related recommendations: javascript video tutorial, web front-end】
The above is the detailed content of What is the usage of yield in es6. For more information, please follow other related articles on the PHP Chinese website!