{"record":{"id":"aaadb84effadc4da","repo":"denoland/deno","slug":"the-iterator-next-method-must-return-an-object","errorCode":null,"errorMessage":"The iterator.next() method must return an object","messagePattern":"The iterator\\.next\\(\\) method must return an object","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"ext/webidl/00_webidl.js","lineNumber":1039,"sourceCode":"  }\n  if (getMethod(obj, SymbolAsyncIterator) !== undefined) {\n    return true;\n  }\n  return getMethod(obj, SymbolIterator) !== undefined;\n}\n\n// https://tc39.es/ecma262/#sec-createasyncfromsynciterator\n// Manual %AsyncFromSyncIteratorPrototype% so we never go through yield* /\n// user-visible @@iterator lookup (primordials-safe).\nfunction createAsyncFromSyncIterator(syncIterator) {\n  // Capture [[NextMethod]] as in GetIteratorDirect / Iterator Record.\n  const nextMethod = syncIterator.next;\n  return {\n    async next() {\n      // IteratorNext(syncIteratorRecord) - sync call, may throw.\n      const iterResult = FunctionPrototypeCall(nextMethod, syncIterator);\n      if (type(iterResult) !== \"Object\") {\n        throw new TypeError(\n          \"The iterator.next() method must return an object\",\n        );\n      }\n      if (iterResult.done) {\n        return { done: true, value: undefined };\n      }\n      // AsyncFromSyncIteratorContinuation awaits the yielded value so that\n      // sync sources of promises (e.g. arrays of Promises) unwrap.\n      return {\n        done: false,\n        value: await iterResult.value,\n      };\n    },\n    async return(reason) {\n      const returnMethod = getMethod(syncIterator, \"return\");\n      if (returnMethod === undefined) {\n        return { done: true, value: undefined };\n      }","sourceCodeStart":1021,"sourceCodeEnd":1057,"githubUrl":"https://github.com/denoland/deno/blob/f7822238cab635a3a19f99f493f675fa81a7f9d8/ext/webidl/00_webidl.js#L1021-L1057","documentation":"When a sync iterable is supplied where an async sequence is required, Deno wraps it in a manual %AsyncFromSyncIteratorPrototype%. Each call to the sync iterator's next() must return an Object (an IteratorResult); primitives throw this TypeError before the value is awaited.","triggerScenarios":"A hand-rolled sync iterator whose next() returns a primitive, e.g. { next: () => 42 } or { next() { return 'done'; } }, used as an async-iterable input such as fetch(url, { body }); also generators delegating with yield* to a broken inner iterator.","commonSituations":"Returning the value directly instead of { done, value }; returning true/false for completion; next() returning undefined after exhaustion instead of { done: true }.","solutions":["Always return { done: boolean, value: any } from next()","Finish with { done: true } (value optional) rather than a bare primitive","Replace the custom iterator with a generator (function*) which always produces valid IteratorResults"],"exampleFix":"// before\nconst body = {\n  [Symbol.iterator]: () => ({ next: () => 'chunk' }), // returns a string\n};\n\n// after\nconst body = {\n  [Symbol.iterator]: () => ({ next: () => ({ done: false, value: 'chunk' }) }),\n};","handlingStrategy":"validation","validationCode":"const it = body[Symbol.iterator]();\nconst first = it.next();\nif (typeof first !== 'object' || first === null) {\n  throw new TypeError('custom iterator next() must return { done, value }');\n}","typeGuard":"const isIteratorResult = (r: unknown): r is IteratorResult<unknown, unknown> =>\n  typeof r === 'object' && r !== null;","tryCatchPattern":null,"preventionTips":["Prefer generators for custom iterables - they always return valid results","Type next() as returning IteratorResult<T>","Unit-test custom iterators: the very first next() call must return an object"],"tags":["webidl","iterator","iterator-result","async-from-sync","typeerror"],"backgroundTag":"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"}