Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

Generator.prototype.return()

BaselineWidely available

Thereturn() method ofGenerator instances acts as if areturn 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 atry...finally block.

Syntax

js
generatorInstance.return()generatorInstance.return(value)

Parameters

valueOptional

The value to return.

Return value

AnObject with two properties:

done

A boolean value:

  • true if the generator function's control flow has reached the end.
  • false if the generator function's control flow hasn't reached the end and can produce more values. This can only happen if thereturn is captured in atry...finally and there are moreyield expressions in thefinally block.
value

The value that is given as an argument, or, if theyield expression is wrapped in atry...finally, the value yielded/returned from thefinally block.

Exceptions

TypeError

Thrown if the generator is already running.

Description

Thereturn() method, when called, can be seen as if areturn value; statement is inserted in the generator's body at the current suspended position, wherevalue is the value passed to thereturn() method. Therefore, in a typical flow, callingreturn(value) will return{ done: true, value: value }. However, if theyield expression is wrapped in atry...finally block, the control flow doesn't exit the function body, but proceeds to thefinally block instead. In this case, the value returned may be different, anddone may even befalse, if there are moreyield expressions within thefinally block.

Examples

Using return()

The following example shows a generator and thereturn method.

js
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 }

Ifreturn(value) is called on a generator that is already in "completed" state, the generator will remain in "completed" state.

If no argument is provided, thevalue property of the returned object will beundefined. If an argument is provided, it will become the value of thevalue property of the returned object, unless theyield expression is wrapped in atry...finally.

js
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 }

Using return() with try...finally

The fact that thereturn method has been called can only be made known to the generator itself if theyield expression is wrapped in atry...finally block.

When thereturn method is called on a generator that is suspended within atry block, execution in the generator proceeds to thefinally block — since thefinally block oftry...finally statements always executes.

js
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 preservedg2.next(); // { value: 'early return', done: true }// Generator is in the completed stateg2.return("not so early return"); // { value: 'not so early return', done: true }

The return value of the finally block can also become thevalue of the result returned from thereturn call.

js
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 }

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-generator.prototype.return

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp