Utility Functions for Callback-based Async Iterators

0.2.2 · abandoned · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

Demonstrates the use of `forEach` with a custom callback-based async iterator, logging each item and an 'end' message.

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.');
});

view raw JSON →