{"record":{"id":"0ac4c41b6659b0f8","repo":"denoland/deno","slug":"opencontext-could-not-be-iterated-because-itera","errorCode":null,"errorMessage":"${openContext} could not be iterated because iterator method did not return object, but ${type(iter)}.","messagePattern":"(.+?) could not be iterated because iterator method did not return object, but (.+?)\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"ext/webidl/00_webidl.js","lineNumber":1126,"sourceCode":"          context,\n        );\n      }\n      sequenceType = \"sync\";\n    }\n\n    return {\n      // Fields used by extractBody / callers.\n      value: V,\n      object: V,\n      method,\n      type: sequenceType,\n      [AsyncSequence]: AsyncSequence,\n      // https://webidl.spec.whatwg.org/#async-sequence-open\n      open(openContext = context) {\n        // 1. Let iterator be ? GetIteratorFromMethod(object, method).\n        const iter = FunctionPrototypeCall(method, V);\n        if (type(iter) !== \"Object\") {\n          throw new TypeError(\n            `${openContext} could not be iterated because iterator method did not return object, but ${\n              type(iter)\n            }.`,\n          );\n        }\n\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() {","sourceCodeStart":1108,"sourceCodeEnd":1144,"githubUrl":"https://github.com/denoland/deno/blob/f7822238cab635a3a19f99f493f675fa81a7f9d8/ext/webidl/00_webidl.js#L1108-L1144","documentation":"WebIDL async-iterable conversion 'open' step: it calls the stored @@iterator/@@asyncIterator method on the value and requires the returned iterator to be an Object. If a primitive comes back, this TypeError includes the API context (openContext) and the actual type name from webidl's type() ('Undefined', 'String', 'Number', ...).","triggerScenarios":"fetch(url, { body: { [Symbol.asyncIterator]: () => 'stream' } }) or any Deno API taking an async sequence where the iterator method returns a primitive instead of an iterator object.","commonSituations":"Writing [Symbol.asyncIterator]() { return this.items } where items is a string/number; async-iterator methods that return a value instead of this or an iterator; confusing 'the method returns the iterator' with 'the method returns the data'.","solutions":["Return an iterator object (usually this) from the @@iterator/@@asyncIterator method","Or delegate: return source[Symbol.asyncIterator]();","Pass a native iterable (Array, Map, Set, ReadableStream) instead of a custom object"],"exampleFix":"// before\nconst body = { [Symbol.asyncIterator]: () => 'not-an-iterator' };\nawait fetch(url, { body });\n\n// after\nconst body = { async *[Symbol.asyncIterator]() { yield 'chunk'; } };\nawait fetch(url, { body });","handlingStrategy":"validation","validationCode":"function assertOpenable(v: unknown) {\n  const m = (v as any)?.[Symbol.asyncIterator] ?? (v as any)?.[Symbol.iterator];\n  if (typeof m !== 'function') return; // absence is a different error\n  const iter = m.call(v);\n  if (typeof iter !== 'object' || iter === null) {\n    throw new TypeError('iterator method must return an iterator object');\n  }\n}","typeGuard":"const isAsyncIterable = (v: unknown): v is AsyncIterable<unknown> =>\n  v != null && typeof (v as Record<PropertyKey, unknown>)[Symbol.asyncIterator] === 'function';","tryCatchPattern":null,"preventionTips":["The @@iterator/@@asyncIterator method must return an iterator (usually this), never the data","Use async generators for fetch bodies","Read the openContext in the message to find which argument failed"],"tags":["webidl","async-iterable","iterator","fetch-body","typeerror"],"backgroundTag":"asynciterator-returns-non-object","analyzedSha":"f7822238cab635a3a19f99f493f675fa81a7f9d8","analyzedAt":"2026-08-20T13:07:44.778Z","contentChangedAt":"2026-08-20T13:07:44.778Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}