asyncGenerator.throw(error) resumes a paused async generator by throwing error at the yield where it stopped, and returns a Promise like next().
If the generator catches the error with a try...catch around that yield, it keeps running and the promise resolves to the next { value, done }. If not, finally blocks run, the generator is finished, and the promise rejects with the same error. Use it to tell a producer that something went wrong, so it can skip an item or retry.
Remember
await gen.throw(new Error("oops"))
caught inside (try/catch around yield)
→ generator keeps running
→ resolves with the next { value, done }
not caught inside
→ finally blocks run, generator is finished
→ the promise rejects with the error
before the first next() → rejects, body never runs
Syntax
Call throw() on a generator that is paused at a yield. Wrap the call in try...catch in case the generator does not handle the error.
JavaScript
const gen = myAsyncGenerator();
await gen.next();
const result = await gen.throw(new Error("message"));
Call
What it does
asyncGenerator.throw(error)
Throw at the paused yield; get a Promise of the next { value, done }.
asyncGenerator.throw(new Error("text"))
Recommended: an Error carries a message and a stack.
await gen.throw(err) in try...catch
Handle the rejection if the generator does not catch it.
Built into JavaScript (ES2018); no import required. Siblings: next() and return().
Library declarations
JavaScript
// AsyncGenerator.prototype.throw (ES2018)
asyncGenerator.throw(exception)
// returns Promise<{ value: any, done: boolean }>
// rejects when the generator does not catch it
Parameters
Parameter
Description
exception
Required in practice. The value to throw inside the generator; an Error such as new Error("timeout") is the clearest choice.
Return Value
Return Value
Description
Promise of { value, done: false }
The generator caught the error and reached another yield.
Promise of { value, done: true }
The generator caught the error and then finished.
Rejected promise
Not caught inside, or not started yet: rejects with the error and the generator is finished.
JavaScript Examples
Example 1 — An Uncaught Error Finishes the Generator
The generator has no catch, so the error comes straight back to the caller after the finally cleanup.
Report a failure.job.throw(new Error("timeout")) raises the error at the paused yield inside the try block.
Recover inside. The generator's catch prints the problem, the for loop moves on to attempt 2, and the promise resolves with that new yielded value instead of rejecting.
Finish normally. The next plain next() continues after the yield, reaches return, and resolves to done: true with the success text.
Example 3 — throw() Before the First next()
A generator that has not started cannot catch anything: the promise rejects and the generator is closed.
rejected: cancelled before start
value: undefined, done: true
How It Works
Not paused anywhere.guarded() has not run a single line, so there is no yield inside the try to throw at.
Straight rejection. The error skips the body completely: neither "body started" nor "inner catch" is printed, and the outer catch receives it.
Closed afterwards. Like an uncaught error, this finishes the generator, so the following next() resolves to value: undefined, done: true.
Compatibility
Browser & Runtime Support
AsyncGenerator.prototype.throw() shipped with async iteration in ES2018 and behaves the same in browsers and server runtimes.
✓ Baseline · ES2018
AsyncGenerator.prototype.throw()
Supported in Chrome 63+, Firefox 55+, 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 throw()Excellent
Bottom line: Safe in every current browser and Node.js LTS release. One runtime difference: an unhandled rejection stops a Node.js 15+ process by default, while browsers only log "Uncaught (in promise)".
Frequently Asked Questions
It resumes a paused async generator by throwing the given value at the yield where it stopped, and returns a Promise. The generator can catch the error and continue, or let it escape, and then the promise rejects.
Its catch block runs and execution continues from there. The promise from throw() resolves with the next { value, done }: the next yielded value, or done: true if the generator then finishes.
Any finally blocks around the yield run, the generator is finished, and the promise rejects with the same error. Later next() calls resolve to { value: undefined, done: true }.
Yes, but the generator cannot catch it because it is not paused inside a try block. The promise rejects right away, the body never runs, and the generator is closed.
When the consumer must tell the producer that something went wrong: a value failed validation, an upload timed out, or the user cancelled a step. The generator can catch it and skip, retry, or log. for await...of only uses next() and return(), never throw().