asyncGenerator.return(value) finishes an async generator from the outside. At a paused yield it acts like a return value; statement: surrounding finally blocks run first (your cleanup), then the Promise resolves to { value, done: true }.
If value is a promise, it is awaited first. for await...of calls return() automatically when you break, return or throw out of the loop; call it yourself when you drive the generator with next() and want to stop early.
Remember
await gen.return("bye") → { value: "bye", done: true }
paused inside try → finally runs first (cleanup)
not started yet → body never runs, done at once
already finished → still { value, done: true }
next() afterwards → { value: undefined, done: true }
for await...of + break → calls return() for you
Syntax
Call return() on the generator object when you want it to stop. Await the result to know that cleanup has finished.
JavaScript
const gen = myAsyncGenerator();
await gen.next();
const result = await gen.return(value);
Call
What it does
asyncGenerator.return()
Finish now; resolves to { value: undefined, done: true }.
asyncGenerator.return(value)
Finish now and report value (a promise is awaited).
for await (const x of gen) { break; }
The loop calls gen.return() for you.
Built into JavaScript (ES2018); no import required. Siblings: next() and throw().
Never started.loadSettings() only created the object; not a single line of its body has run yet.
Promise argument.return(Promise.resolve("cancelled")) awaits that promise, so the result holds the plain string "cancelled".
Nothing to clean up. Neither "loading settings..." nor "finally ran" is printed: the try block was never entered, so its finally has nothing to do.
Compatibility
Browser & Runtime Support
AsyncGenerator.prototype.return() is part of async iteration in ES2018. The automatic call on break comes from for await...of, added in the same edition.
✓ Baseline · ES2018
AsyncGenerator.prototype.return()
Supported in Chrome 63+, Firefox 57+ (with for await...of), Safari 12+, Edge 79+, and Node.js 10+. Not available in Internet Explorer.
96%Modern browser support
Google ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
BunSupported · JavaScript runtime
Supported
DenoSupported · JavaScript runtime
Supported
Node.jsSupported · Server runtime
Supported
Android WebViewSupported · Modern WebView
Full support
AsyncGenerator return()Excellent
Bottom line: Safe in every current browser and Node.js LTS release. Early engines differed in small details, such as when a promise passed to return() is awaited; current engines agree, and the outputs above come from Node.js 24.
Frequently Asked Questions
It finishes the generator from the outside. At a paused yield it acts like a return statement: finally blocks run, then the promise resolves to { value, done: true }. Later next() calls resolve to { value: undefined, done: true }.
Yes, when the generator is paused inside a try block that has a finally, which is the main reason to use it: closing files, connections or timers. If the generator has not started yet, nothing in its body runs.
Yes. Leaving the loop early with break, return, or an error makes for await...of call return() and wait for it. When the loop ends normally the generator is already finished, so no call is needed.
The argument you pass, wrapped in { value, done: true }. A promise argument is awaited first. With no argument, value is undefined.
Not immediately. The request is queued until the current await finishes and the generator reaches its next yield; the return happens there. To cancel the slow operation itself, the awaited code must support cancellation, for example with an AbortController signal.