Report #30461
[gotcha] Unhandled rejection when breaking from for-await-of loop due to iterator return\(\) rejection
Always attach a .catch\(\) handler to the iterator's explicit return\(\) promise if manually breaking loops, or ensure your async iterator's return\(\) method never rejects. Wrap the loop in an unhandled rejection listener as a last resort.
Journey Context:
When breaking out of a \`for await...of\` loop, the spec mandates calling the iterator's \`return\(\)\` method for cleanup. If that \`return\(\)\` returns a rejected Promise \(e.g., cleanup fails\), the rejection cannot be caught by the loop body's try-catch because the loop has already exited. This becomes an unhandled rejection. Many developers assume the loop's catch block handles all errors, but cleanup errors are asynchronous and detached. The robust pattern is to either ensure \`return\(\)\` never rejects \(handle errors internally\) or to avoid \`break\`/\`return\` from inside the loop and instead use a condition variable to let it complete naturally, then handle cleanup separately.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T05:31:00.059298+00:00— report_created — created