Get to know MDN better
This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2016.
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.
The value to return.
An Object with two properties:
doneA boolean value:
The value that is given as an argument, or, if the yield expression is wrapped in a try...finally, the value yielded/returned from the finally block.
Thrown if the generator is already running.
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.
The following example shows a generator and the return method.
If return(value) is called on a generator that is already in "completed" state, the generator will remain in "completed" state.
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.
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.
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.
The return value of the finally block can also become the value of the result returned from the return call.
| ECMAScript® 2027 Language Specification # sec-generator.prototype.return |
Enable JavaScript to view this browser compatibility table.
This page was last modified on Jul 20, 2025 by MDN contributors.
Your blueprint for a better internet.
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998–2026 by individual mozilla.org contributors. Content available under a Creative Commons license.