Utility Functions for Callback-based Async Iterators
This package provides utility functions like `forEach`, `map`, and `filter` for a specific, custom, callback-based asynchronous iterator pattern. In this library's context, an async iterator is defined as an object with a `next(cb)` method, where `cb` is a `function(err, value)` callback. The `next` method should return the next item from an underlying data source, calling `cb(null, undefined)` when no more data is available. Published in 2013, its current and only stable version is 0.2.2. The library targets legacy Node.js versions (>=0.10) and significantly predates the native ECMAScript `Symbol.asyncIterator` and `for-await-of` syntax, which were standardized in ES2018 and natively supported in Node.js 10.x. As it has seen no updates in over a decade, it is considered abandoned and fundamentally incompatible with modern asynchronous programming paradigms without extensive re-engineering or wrappers.
Common errors
-
TypeError: iterators.forEach is not a function
cause Attempting to use `async-iterators` in an ES module context with `import` syntax, or the variable holding the `require` result is not named `iterators` or correctly destructured.fixEnsure you are using CommonJS `require` syntax: `const iterators = require('async-iterators');`. -
ReferenceError: require is not defined in ES module scope
cause Trying to use `require` in a JavaScript file explicitly defined as an ES module (e.g., via `"type": "module"` in `package.json` or `.mjs` extension).fixThis package is not compatible with ES module projects. You must either convert your project to CommonJS or choose a modern async iterator library that supports ES modules.
Warnings
- breaking This package uses a custom callback-based async iterator pattern that is fundamentally different from the native ECMAScript `Symbol.asyncIterator` protocol (ES2018). It is not compatible with `for-await-of` loops or modern Promise-based async/await syntax.
- gotcha The package was last updated over a decade ago (version 0.2.2 published in 2013) and targets Node.js versions >=0.10. It is highly unlikely to be compatible with current Node.js runtimes (e.g., Node.js 16+ or 20+) without significant environment setup or polyfills for ancient Node.js APIs.
- gotcha This library is CommonJS-only and does not support ES Modules (`import`/`export` syntax). Attempting to import it directly in an ESM context will result in errors.
Install
-
npm install async-iterators -
yarn add async-iterators -
pnpm add async-iterators
Imports
- iterators
import * as iterators from 'async-iterators'
const iterators = require('async-iterators') - forEach
import { forEach } from 'async-iterators'const { forEach } = require('async-iterators') - map
import { map } from 'async-iterators'const map = require('async-iterators').map
Quickstart
const iterators = require('async-iterators');
// A dummy async iterator following the package's callback-based pattern
function createExampleIterator() {
let i = 0;
const data = [10, 20, 30, 40, 50];
return {
next: function(cb) {
if (i < data.length) {
setTimeout(() => {
cb(null, data[i]);
i++;
}, 50);
} else {
cb(null, undefined); // Signal end of iteration
}
}
};
}
const myIterator = createExampleIterator();
console.log('Starting iteration...');
iterators.forEach(myIterator, function(err, data) {
if (err) {
console.error('Error:', err);
return;
}
if (data !== undefined) {
console.log('Received data:', data);
}
}, function() {
console.log('End of iteration.');
});