1.0.7 • Published 5 years ago

iterable-async-stream v1.0.7

Weekly downloads
2
License
MIT
Repository
github
Last release
5 years ago

iterable-async-stream

An async stream which can be iterated over using a for-await-of loop.

Installation

npm install iterable-async-stream

Usage

Consume a stream and write to it asynchronously:

let iterableStream = new IterableAsyncStream();

async function consumeAsyncIterable(iterable) {
  // Consume iterable data asynchronously.
  for await (let packet of iterable) {
    console.log('Packet:', packet);
  }
}
consumeAsyncIterable(iterableStream);

setInterval(() => {
  // Write data to the stream asynchronously,
  iterableStream.write(`Timestamp: ${Date.now()}`);
}, 100);

Consume a filtered stream using an async generator:

let iterableStream = new IterableAsyncStream();

// Creates an async generator which only produces packets which are allowed by the
// specified filterFunction.
async function* createFilteredStreamGenerator(fullStream, filterFunction) {
  for await (let packet of fullStream) {
    if (filterFunction(packet)) {
      yield packet;
    }
  }
};

async function consumeAsyncIterable(iterable) {
  // Consume iterable data asynchronously.
  for await (let packet of iterable) {
    console.log('Packet:', packet);
  }
}

// The filter function will only include strings which end with the number 5.
function filterFn(data) {
  return /5$/.test(data);
}
let filteredStreamGenerator = createFilteredStreamGenerator(iterableStream, filterFn);

consumeAsyncIterable(filteredStreamGenerator);

setInterval(() => {
  // Write data to the stream asynchronously,
  iterableStream.write(`Timestamp: ${Date.now()}`);
}, 100);

See test/ directory for additional examples.

1.0.7

5 years ago

1.0.6

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago