{"record":{"id":"421918e90b75f8fb","repo":"denoland/deno","slug":"opencontext-failed-to-close-iterator-because-th","errorCode":null,"errorMessage":"${openContext} failed to close iterator because the return() method did not return an object, but ${type(returnPromiseResult)}.","messagePattern":"(.+?) failed to close iterator because the return\\(\\) method did not return an object, but (.+?)\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"ext/webidl/00_webidl.js","lineNumber":1183,"sourceCode":"              opts,\n            );\n\n            return { done: false, value: iterValue };\n          },\n          // https://webidl.spec.whatwg.org/#async-iterator-close\n          async return(reason) {\n            const returnMethod = getMethod(asyncIterator, \"return\");\n            if (returnMethod === undefined) {\n              return undefined;\n            }\n\n            const returnPromiseResult = await FunctionPrototypeCall(\n              returnMethod,\n              asyncIterator,\n              reason,\n            );\n            if (type(returnPromiseResult) !== \"Object\") {\n              throw new TypeError(\n                `${openContext} failed to close iterator because the return() method did not return an object, but ${\n                  type(returnPromiseResult)\n                }.`,\n              );\n            }\n\n            return undefined;\n          },\n          [SymbolAsyncIterator]() {\n            return this;\n          },\n        };\n      },\n      // Allow for-await-of over the converted async sequence directly.\n      [SymbolAsyncIterator]() {\n        return this.open(context);\n      },\n    };","sourceCodeStart":1165,"sourceCodeEnd":1201,"githubUrl":"https://github.com/denoland/deno/blob/f7822238cab635a3a19f99f493f675fa81a7f9d8/ext/webidl/00_webidl.js#L1165-L1201","documentation":"When a WebIDL async-sequence consumer closes the iterator early (break, error, abort), it looks up the iterator's optional return(); if present, the awaited result must be an Object. Otherwise this TypeError is thrown, including the API context (openContext) and the offending type name.","triggerScenarios":"An async-iterable fetch body whose return() resolves to a non-object - return() { } (undefined), async return() { await cleanup() } where cleanup returns a string, or return: async () => null - followed by early termination (AbortController, downstream error, read limit).","commonSituations":"Cleanup hooks that forget to return a value; returning a boolean success flag from return(); return() that resolves to a resource handle number.","solutions":["Return { done: true } from return()","Or omit return() - an absent method means no close is attempted","When delegating, return the inner iterator's result object"],"exampleFix":"// before\nasync return() { await cleanup(); } // resolves to undefined\n\n// after\nasync return() { await cleanup(); return { done: true }; }","handlingStrategy":"validation","validationCode":null,"typeGuard":"interface CloseableAsyncIterator<T> extends AsyncIterator<T> {\n  return?(value?: any): Promise<IteratorResult<T>>;\n}\n// Typing return() as Promise<IteratorResult<T>> prevents resolving to primitives.","tryCatchPattern":null,"preventionTips":["return() must resolve to an object - conventionally { done: true }","Test abort/cancel paths of custom async iterables","When wrapping an inner iterator, return its result object from return()"],"tags":["webidl","async-iterator","cleanup","cancellation","typeerror"],"backgroundTag":"async-iterator-return-returns-non-object","analyzedSha":"f7822238cab635a3a19f99f493f675fa81a7f9d8","analyzedAt":"2026-08-20T13:07:44.778Z","contentChangedAt":"2026-08-20T13:07:44.778Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}