{"record":{"id":"8f43c343b5e55766","repo":"denoland/deno","slug":"opencontext-failed-to-iterate-next-value-becaus","errorCode":null,"errorMessage":"${openContext} failed to iterate next value because the next() method did not return an object, but ${type(iterResult)}.","messagePattern":"(.+?) failed to iterate next value because the next\\(\\) method did not return an object, but (.+?)\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"ext/webidl/00_webidl.js","lineNumber":1150,"sourceCode":"\n        // 2. If type is \"sync\", set iterator to CreateAsyncFromSyncIterator(iterator).\n        const asyncIterator = sequenceType === \"sync\"\n          ? createAsyncFromSyncIterator(iter)\n          : iter;\n\n        // Capture nextMethod per Iterator Record semantics.\n        const nextMethod = asyncIterator.next;\n\n        return {\n          // https://webidl.spec.whatwg.org/#async-iterator-get-next-value\n          // Exposed as an async iterator protocol shape for callers.\n          async next() {\n            const iterResult = await FunctionPrototypeCall(\n              nextMethod,\n              asyncIterator,\n            );\n            if (type(iterResult) !== \"Object\") {\n              throw new TypeError(\n                `${openContext} failed to iterate next value because the next() method did not return an object, but ${\n                  type(iterResult)\n                }.`,\n              );\n            }\n\n            if (iterResult.done) {\n              return { done: true, value: undefined };\n            }\n\n            const iterValue = converter(\n              iterResult.value,\n              `${openContext} failed to iterate next value`,\n              \"The value returned from the next() method\",\n              opts,\n            );\n\n            return { done: false, value: iterValue };","sourceCodeStart":1132,"sourceCodeEnd":1168,"githubUrl":"https://github.com/denoland/deno/blob/f7822238cab635a3a19f99f493f675fa81a7f9d8/ext/webidl/00_webidl.js#L1132-L1168","documentation":"While iterating a WebIDL async sequence, every awaited result of the captured next() method must be an Object ({ done, value }). This TypeError names the API context (openContext) and the offending type (e.g. 'Number') when next() resolves to a primitive.","triggerScenarios":"A custom async iterator with next: async () => 42 or next() { return Promise.resolve('x'); } used as an async-sequence input (e.g. fetch body); next() returning undefined after exhaustion.","commonSituations":"Async next() implementations returning the value directly instead of { value, done }; wrapper/mapping code that transforms the iterator result; returning done: true as a bare boolean.","solutions":["Wrap every result: return { done: false, value } and return { done: true } at the end","Replace the manual iterator with an async generator (async function*)","Read the openContext in the message to identify which API argument failed"],"exampleFix":"// before\nconst body = { [Symbol.asyncIterator]: () => ({ next: async () => 'chunk' }) };\n\n// after\nconst body = { async *[Symbol.asyncIterator]() { yield 'chunk'; } };","handlingStrategy":"validation","validationCode":"const it = body[Symbol.asyncIterator]();\nconst r = await it.next();\nif (typeof r !== 'object' || r === null) {\n  throw new TypeError('async next() must resolve to { done, value }');\n}","typeGuard":"const isAsyncIteratorResult = (r: unknown): r is IteratorResult<unknown> =>\n  typeof r === 'object' && r !== null;","tryCatchPattern":null,"preventionTips":["Async iterators resolve to IteratorResult objects, never raw values","Use async generators instead of hand-written next()","Type custom async iterators as AsyncIterator<T>"],"tags":["webidl","async-iterator","iterator-result","typeerror"],"backgroundTag":"async-iterator-next-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"}