{"id":15228,"library":"response-iterator","title":"Response Iterator","description":"response-iterator is a utility library designed to normalize various HTTP response bodies into a consistent asynchronous iterator interface, compatible across both browser and Node.js environments. It currently stands at stable version 1.0.10 and is regularly maintained. The library abstracts away the differences in how `Response` objects (or similar constructs) are handled by different HTTP clients such as standard `fetch`, `node-fetch`, `cross-fetch`, `axios`, `got`, and `undici`. Its key differentiator is providing a simple `for await...of` loop mechanism to consume streamed data chunks, making it easier to process large responses efficiently without loading the entire body into memory, irrespective of the underlying fetch implementation. It significantly simplifies working with streaming data by adhering to the `Symbol.asyncIterator` protocol.","status":"active","version":"1.0.10","language":"javascript","source_language":"en","source_url":"ssh://git@github.com/kmalakoff/response-iterator","tags":["javascript","Symbol","Symbol.asyncIterator","array","arrayBuffer","asyncIterator","axios","blob","body","typescript"],"install":[{"cmd":"npm install response-iterator","lang":"bash","label":"npm"},{"cmd":"yarn add response-iterator","lang":"bash","label":"yarn"},{"cmd":"pnpm add response-iterator","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The package is ESM-only since version 1.x. Using `require()` in a CommonJS module will result in an `ERR_REQUIRE_ESM` error. Ensure your environment supports ESM or use dynamic import.","wrong":"const responseIterator = require('response-iterator');","symbol":"responseIterator","correct":"import responseIterator from 'response-iterator';"}],"quickstart":{"code":"import responseIterator from 'response-iterator';\n\n// A polyfill like 'isomorphic-fetch' or 'node-fetch' might be needed for Node.js environments\n// if a global `fetch` is not available. For browser, `fetch` is native.\n\nasync function fetchData() {\n  try {\n    const res = await fetch('https://raw.githubusercontent.com/kmalakoff/response-iterator/master/package.json');\n    if (!res.ok) {\n      throw new Error(`HTTP error! status: ${res.status}`);\n    }\n\n    let data = '';\n    for await (const chunk of responseIterator(res)) {\n      data += chunk;\n    }\n    console.log('Package name:', JSON.parse(data).name); // Expected: \"response-iterator\"\n  } catch (error) {\n    console.error('Failed to fetch or process data:', error);\n  }\n}\n\nfetchData();","lang":"typescript","description":"Demonstrates fetching a JSON file and consuming its content as an async iterator."},"warnings":[{"fix":"Consult the project's GitHub release history and examples. Re-evaluate direct usage of internal APIs or patterns that might have changed.","message":"Major version 1.0.0 introduces breaking changes. While specific release notes for 0.x to 1.x are not readily available, according to semantic versioning, a jump to `1.0.0` signifies that the API is now considered stable, but with potential breaking changes from any `0.x` version. Always review your usage when upgrading from `0.x` to `1.x`.","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"Migrate your consuming code to use ES Modules (`import responseIterator from 'response-iterator';`) or use dynamic `import()` within CommonJS. Ensure your Node.js environment or bundler is configured for ESM.","message":"The package transitioned to an ESM-only distribution model with its `1.x` release. Attempting to `require()` this package in a CommonJS module will fail.","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"Ensure the function consuming the iterator is declared `async` and the iteration uses `for await (const chunk of iterator)`. Handle potential errors within the loop or with a `try...catch` block around the async operation.","message":"Asynchronous iterators, including those produced by `response-iterator`, must be consumed within an `async` function using a `for await...of` loop. Forgetting `await` or not being in an `async` context will lead to runtime errors or unhandled promises.","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"Always wrap the `fetch` call and the `for await...of` loop in a `try...catch` block. Implement robust error handling for network issues, parsing errors, and any custom logic applied to chunks.","message":"When handling network responses, especially large streams, it's crucial to properly handle errors that might occur during the stream's lifetime. An unhandled exception in the underlying `Response` stream or during chunk processing can cause the consuming `for await...of` loop to hang or terminate unexpectedly without proper cleanup.","severity":"gotcha","affected_versions":">=0.1.0"}],"env_vars":null,"last_verified":"2026-04-21T00:00:00.000Z","next_check":"2026-07-20T00:00:00.000Z","problems":[{"fix":"Change `const responseIterator = require('response-iterator');` to `import responseIterator from 'response-iterator';`. Ensure your project's `package.json` has `\"type\": \"module\"` or use dynamic imports like `const responseIterator = (await import('response-iterator')).default;`.","cause":"Attempting to import `response-iterator` using `require()` in a CommonJS environment, but the package is ESM-only.","error":"TypeError [ERR_REQUIRE_ESM]: require() of ES Module .../node_modules/response-iterator/dist/index.js from ... not supported."},{"fix":"Verify the import statement is `import responseIterator from 'response-iterator';` (for a default export). If in CommonJS, confirm dynamic import usage: `const responseIterator = (await import('response-iterator')).default;`.","cause":"This usually happens if `responseIterator` is imported incorrectly, or if the module itself failed to load or initialize. It could also mean `responseIterator` was imported as a named import when it's a default export.","error":"TypeError: responseIterator is not a function"},{"fix":"Ensure the function containing `for await (const chunk of responseIterator(res))` is declared `async`. Also, verify that `responseIterator(res)` is indeed returning an async iterable and not a plain value or a Promise of a value that needs further resolution.","cause":"This error occurs when `await` is used on a non-Promise value, or when an `async` function tries to iterate over an async iterable without `for await...of` in an `async` context, or a synchronous iterable is treated as async.","error":"TypeError: (intermediate value).then is not a function"}],"ecosystem":"npm"}