async-iterable-sync-iterator
TypeScript icon, indicating that this package has built-in type declarations

0.1.0 • Public • Published

Asynchronously iterable (synchronous) iterator

Utility to create a regular (synchronous) iterator that provides promises as values, allowing for asynchronous iteration without requiring the ECMAScript async iteration standard.

The ECMAScript spec will not allow iterations to fail without terminating the iterator, while this iterator enables asynchronous iteration with failure as an acceptable and catchable possible scenario for every single iteration.

It can be used to make a stream of fallible events asynchronously iterable, for instance when running a compiler in watch mode:

const { createIterator } = require('async-iterable-sync-iterator');
 
async function main() {
 
  const iterator = createIterator();
 
  watchCompile((err, result) => {
    if (err) {
      // Let's assume the compiler's Error objects have a property that
      // indicates whether the error is fatal or not.
      if (err.isFatal)
        iterator.end();
      iterator.reject(err);
    } else {
      iterator.resolve(result);
    }
  });
 
  for (const value of iterator) {
 
    console.log("Awaiting next compile…");
 
    let result;
    try {
      result = await value;
    } catch (err) {
      console.error("Compile failed", err);
      continue;
    }
 
    console.log("Compile succeeded", result);
 
  }
 
}
 
main();

Support

Has been developed and tested against Node 8, but it is very likely to also work fine on older versions.

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 0.1.0
    0
    • latest

Version History

  • Version
    Downloads (Last 7 Days)
    • Published
  • 0.1.0
    0

Package Sidebar

Install

npm i async-iterable-sync-iterator

Weekly Downloads

0

Version

0.1.0

License

ISC

Last publish

Collaborators

  • timmolendijk