Generator.prototype.return()
Generator
实例的 return()
方法的作用就好像将 return
语句插入到生成器主体中当前挂起的位置,这会完成生成器并允许生成器在与 try...finally
块组合时执行任何清理任务。
¥The return()
method of Generator
instances acts as if a return
statement is inserted in the generator's body at the current suspended position, which finishes the generator and allows the generator to perform any cleanup tasks when combined with a try...finally
block.
语法
参数
返回值
¥Return value
Object
有两个属性:
¥An Object
with two properties:
done
-
布尔值:
- 如果生成器函数的控制流已到达末尾,则为
true
。 false
如果生成器函数的控制流尚未到达末尾并且可以产生更多值。仅当return
被捕获在try...finally
中并且finally
块中有更多yield
表达式时,才会发生这种情况。
- 如果生成器函数的控制流已到达末尾,则为
value
-
作为参数给出的值,或者,如果
yield
表达式包含在try...finally
中,则为从finally
块产生/返回的值。
描述
¥Description
调用 return()
方法时,可以将其视为在生成器主体中当前挂起位置插入了一条 return value;
语句,其中 value
是传递给 return()
方法的值。因此,在典型的流程中,调用 return(value)
将返回 { done: true, value: value }
。但是,如果 yield
表达式包含在 try...finally
块中,则控制流不会退出函数体,而是进入 finally
块。在这种情况下,如果 finally
块中有更多 yield
表达式,则返回的值可能会不同,并且 done
甚至可能是 false
。
¥The return()
method, when called, can be seen as if a return value;
statement is inserted in the generator's body at the current suspended position, where value
is the value passed to the return()
method. Therefore, in a typical flow, calling return(value)
will return { done: true, value: value }
. However, if the yield
expression is wrapped in a try...finally
block, the control flow doesn't exit the function body, but proceeds to the finally
block instead. In this case, the value returned may be different, and done
may even be false
, if there are more yield
expressions within the finally
block.
示例
使用 return()
¥Using return()
以下示例显示了一个简单的生成器和 return
方法。
¥The following example shows a simple generator and the return
method.
function* gen() {
yield 1;
yield 2;
yield 3;
}
const g = gen();
g.next(); // { value: 1, done: false }
g.return("foo"); // { value: "foo", done: true }
g.next(); // { value: undefined, done: true }
如果在已经处于 "completed" 状态的生成器上调用 return(value)
,生成器将保持在 "completed" 状态。
¥If return(value)
is called on a generator that is already in "completed" state, the generator will remain in "completed" state.
如果未提供参数,则返回对象的 value
属性将为 undefined
。如果提供了参数,它将成为返回对象的 value
属性的值,除非 yield
表达式封装在 try...finally
中。
¥If no argument is provided, the value
property of the returned object will be undefined
. If an argument is provided, it will become the value of the value
property of the returned object, unless the yield
expression is wrapped in a try...finally
.
function* gen() {
yield 1;
yield 2;
yield 3;
}
const g = gen();
g.next(); // { value: 1, done: false }
g.next(); // { value: 2, done: false }
g.next(); // { value: 3, done: false }
g.next(); // { value: undefined, done: true }
g.return(); // { value: undefined, done: true }
g.return(1); // { value: 1, done: true }
将 return() 与 try...finally 一起使用
¥Using return() with try...finally
如果 yield
表达式封装在 try...finally
块中,则只有生成器本身才能知道 return
方法已被调用的事实。
¥The fact that the return
method has been called can only be made known to the generator itself if the yield
expression is wrapped in a try...finally
block.
当在 try
块内挂起的生成器上调用 return
方法时,生成器中的执行将继续到 finally
块 - 因为 try...finally
语句的 finally
块始终执行。
¥When the return
method is called on a generator that is suspended within a try
block, execution in the generator proceeds to the finally
block — since the finally
block of try...finally
statements always executes.
function* gen() {
yield 1;
try {
yield 2;
yield 3;
} finally {
yield "cleanup";
}
}
const g1 = gen();
g1.next(); // { value: 1, done: false }
// Execution is suspended before the try...finally.
g1.return("early return"); // { value: 'early return', done: true }
const g2 = gen();
g2.next(); // { value: 1, done: false }
g2.next(); // { value: 2, done: false }
// Execution is suspended within the try...finally.
g2.return("early return"); // { value: 'cleanup', done: false }
// The completion value is preserved
g2.next(); // { value: 'early return', done: true }
// Generator is in the completed state
g2.return("not so early return"); // { value: 'not so early return', done: true }
finally 块的返回值也可以成为 return
调用返回结果的 value
。
¥The return value of the finally block can also become the value
of the result returned from the return
call.
function* gen() {
try {
yield 1;
} finally {
return "cleanup";
}
}
const g1 = gen();
g1.next(); // { value: 1, done: false }
g1.return("early return"); // { value: 'cleanup', done: true }
规范
Specification |
---|
ECMAScript Language Specification # sec-generator.prototype.return |
浏览器兼容性
BCD tables only load in the browser
也可以看看
¥See also