yield

yield 运算符用于暂停和恢复 生成器函数

¥The yield operator is used to pause and resume a generator function.

Try it

语法

¥Syntax

js
yield
yield expression

参数

¥Parameters

expression Optional

通过 迭代器协议 从生成器函数产生的值。如果省略,则生成 undefined

返回值

¥Return value

返回传递给生成器的 next() 方法以恢复其执行的可选值。

¥Returns the optional value passed to the generator's next() method to resume its execution.

注意:这意味着 next() 是不对称的:它总是向当前挂起的 yield 发送一个值,但返回下一个 yield 的操作数。无法检索传递给第一个 next() 调用的参数,因为当前没有挂起的 yield

¥Note: This means next() is asymmetric: it always sends a value to the currently suspended yield, but returns the operand of the next yield. The argument passed to the first next() call cannot be retrieved because there's no currently suspended yield.

描述

¥Description

yield 关键字暂停生成器函数执行,并将 yield 关键字后面的表达式的值返回给生成器的调用者。它可以被认为是 return 关键字的基于生成器的版本。

¥The yield keyword pauses generator function execution and the value of the expression following the yield keyword is returned to the generator's caller. It can be thought of as a generator-based version of the return keyword.

yield 只能在包含它的生成器函数中直接使用。它不能在嵌套函数中使用。

¥yield can only be used directly within the generator function that contains it. It cannot be used within nested functions.

调用生成器函数构造一个 Generator 对象。每次调用生成器的 next() 方法时,生成器都会恢复执行,并运行直到达到以下条件之一:

¥Calling a generator function constructs a Generator object. Each time the generator's next() method is called, the generator resumes execution, and runs until it reaches one of the following:

  • yield 表达式。在这种情况下,生成器暂停,并且 next() 方法返回具有两个属性的 迭代器结果 对象:valuedonevalue 属性是 yield 运算符之后表达式的值,donefalse,表明生成器函数还没有完全完成。
  • 生成器函数结束。在这种情况下,生成器的执行结束,next() 方法返回一个迭代器结果对象,其中 valueundefineddonetrue
  • return 声明。在这种情况下,生成器的执行结束,next() 方法返回一个迭代器结果对象,其中 value 是指定的返回值,donetrue
  • throw 声明。在这种情况下,生成器的执行完全停止,并且 next() 方法抛出指定的异常。

一旦在 yield 表达式上暂停,生成器的代码执行将保持暂停状态,直到再次调用生成器的 next() 方法。如果将可选值传递给生成器的 next() 方法,则该值将成为生成器当前 yield 操作返回的值。第一个 next() 调用没有相应的挂起 yield 操作,因此无法将参数传递给第一个 next() 调用。

¥Once paused on a yield expression, the generator's code execution remains paused until the generator's next() method is called again. If an optional value is passed to the generator's next() method, that value becomes the value returned by the generator's current yield operation. The first next() call does not have a corresponding suspended yield operation, so there's no way to get the argument passed to the first next() call.

如果调用生成器的 return()throw() 方法,则其行为就像在暂停的 yield 表达式处执行 returnthrow 语句一样。你可以在生成器函数体内使用 try...catch...finally 来处理这些早期完成。如果调用 return()throw() 方法但没有挂起的 yield 表达式(因为尚未调用 next(),或者因为生成器已经完成),则无法处理早期完成并始终终止生成器。

¥If the generator's return() or throw() method is called, it acts as if a return or throw statement was executed at the paused yield expression. You can use try...catch...finally within the generator function body to handle these early completions. If the return() or throw() method is called but there's no suspended yield expression (because next() has not been called yet, or because the generator has already completed), then the early completions cannot be handled and always terminate the generator.

示例

¥Examples

使用产量

¥Using yield

以下代码是示例生成器函数的声明。

¥The following code is the declaration of an example generator function.

js
function* countAppleSales() {
  const saleList = [3, 7, 5];
  for (let i = 0; i < saleList.length; i++) {
    yield saleList[i];
  }
}

一旦定义了生成器函数,就可以通过构造迭代器来使用它,如下所示。

¥Once a generator function is defined, it can be used by constructing an iterator as shown.

js
const appleStore = countAppleSales(); // Generator { }
console.log(appleStore.next()); // { value: 3, done: false }
console.log(appleStore.next()); // { value: 7, done: false }
console.log(appleStore.next()); // { value: 5, done: false }
console.log(appleStore.next()); // { value: undefined, done: true }

你还可以将带有 next(value) 的值发送到生成器中。step 计算为 yield 表达式的返回值 — 尽管忽略第一次调用 next() 时传递给生成器的 next() 方法的值。

¥You can also send a value with next(value) into the generator. step evaluates as a return value of the yield expression — although the value passed to the generator's next() method the first time next() is called is ignored.

js
function* counter(value) {
  while (true) {
    const step = yield value++;

    if (step) {
      value += step;
    }
  }
}

const generatorFunc = counter(0);
console.log(generatorFunc.next().value); // 0
console.log(generatorFunc.next().value); // 1
console.log(generatorFunc.next().value); // 2
console.log(generatorFunc.next().value); // 3
console.log(generatorFunc.next(10).value); // 14
console.log(generatorFunc.next().value); // 15
console.log(generatorFunc.next(10).value); // 26

规范

Specification
ECMAScript Language Specification
# prod-YieldExpression

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看