return()ends a generator early. You call it when you are happy with the values you have and do not want the rest. The generator stops at the point where it is paused and is marked as finished.
It returns { value, done: true }, where value is the argument you passed. If the generator was paused inside a try block, its finally code still runs, which makes return() the right way to let a generator clean up.
Remember
const result = generator.return(finalValue);
result.value → finalValue (or undefined)
result.done → always true
The generator is finished: next() now gives
{ value: undefined, done: true }.
finally blocks inside the generator still run.
Syntax
Call return() on a generator object. The argument is optional.
JavaScript
generator.return();
generator.return(finalValue);
Call
What it does
gen.return()
Finish the generator. Returns { value: undefined, done: true }.
gen.return("stop")
Finish the generator and use "stop" as the final value.
for (const x of gen) { break; }
Leaving a for...of loop early with break calls return() for you.
Built into JavaScript (ES2015). Every generator object has it; no import required. Siblings: next() and throw().
Open and read. The first next() prints "file opened", enters the try block and pauses at yield "line 1".
return() jumps to finally. Instead of continuing, gen.return("early exit") makes the generator leave the try block. The finally code runs and prints "file closed"before the result line.
Finished for good. The returned value is "early exit", and a later next() reports done: true.
Example 3 — See break and Not-Started Generators
A break in a for...of loop calls return() for you. A generator that never started behaves differently.
a
b
cleanup ran
value: never started, done: true
value: undefined, done: true
How It Works
break calls return(). When the loop stops at "b", JavaScript calls return() on the generator for you. That is why "cleanup ran" is printed even though you never wrote return().
A fresh generator.fresh was created but never started with next(), so the code inside it never ran, and neither did its try or finally.
It is closed anyway.fresh.return("never started") gives done: true with your value, and next() afterwards has nothing left to give. No "cleanup ran" is printed a second time.
Compatibility
Browser & Runtime Support
Generators arrived with ES2015. Every current browser and Node.js version supports them, including return().
✓ ES2015 · Widely supported
generator.return()
Supported in Chrome 39+, Edge 13+, Firefox 26+, Safari 10+ and current Node.js, Deno and Bun. Internet Explorer never got generators.
ES2015Standard since
Google Chrome39+ · Desktop & Mobile
Full support
Microsoft Edge13+ · Legacy & Chromium
Full support
Mozilla Firefox26+ · Desktop & Mobile
Full support
Apple Safari10+ · macOS & iOS
Full support
Opera26+ · Chromium based
Full support
Samsung InternetRecent versions · Android
Supported
Android WebViewRecent versions · Chromium based
Supported
Node.jsAll current versions · Server runtime
Supported
DenoAll versions · JavaScript runtime
Supported
BunAll versions · JavaScript runtime
Supported
Internet ExplorerNo native support
Not supported
generator.return()Excellent
Bottom line: Safe to use in every current browser and in Node.js. Only Internet Explorer lacks function*, and it is no longer supported.
Frequently Asked Questions
It ends the generator early. The generator stops where it is paused, its finally blocks run, and the call returns { value, done: true } with the value you passed. After that, next() only returns { value: undefined, done: true }.
They have a similar effect but come from different places. The return statement is written inside the generator and ends it from the inside. The return() method is called from outside, on the generator object, and ends the generator from the outside.
Yes. When you leave a for...of loop early with break (or with return or an error), JavaScript calls return() on the generator for you. That is how finally blocks in generators get their chance to clean up.
Yes, if the generator is paused inside a try block that has a finally. The finally code runs first and the result comes afterwards. If the generator has not started yet, or is already finished, there is nothing to clean up and nothing runs.
Always { value: undefined, done: true }. A generator cannot be restarted. To get the values again, call the generator function once more to create a new generator object.