async-iterable-to-readable-stream
v1.0.0
Published
[![CircleCI](https://circleci.com/gh/bunchtogether/async-iterable-to-readable-stream.svg?style=svg)](https://circleci.com/gh/bunchtogether/async-iterable-to-readable-stream) [![npm version](https://badge.fury.io/js/async-iterable-to-readable-stream.svg)](
Downloads
3
Readme
Async Iterable to Readable Stream
Convert an async iterable into a readable stream. No dependencies.
If you encounter an issue, fork the repository, write tests demonstrating the issue, and create a pull request.
const asyncIterableToReadableStream = require('async-iterable-to-readable-stream');
const crypto = require('crypto');
const chunks = [
crypto.randomBytes(Math.round(Math.random() * 256)),
crypto.randomBytes(Math.round(Math.random() * 256)),
crypto.randomBytes(Math.round(Math.random() * 256)),
crypto.randomBytes(Math.round(Math.random() * 256)),
];
async function* getChunks() {
for (const item of chunks) {
await new Promise((resolve) => setImmediate(resolve));
yield item;
}
}
const iterable = getChunks();
const stream = asyncIterableToReadableStream(iterable);
stream.on('data', (chunk) => {
console.log(`Chunk with length ${chunk.length}`);
});
stream.on('error', (error) => {
console.error(error);
});
stream.on('end', () => {
console.log("End");
});
stream.on('close', () => {
console.log("Close");
});
Install
yarn add async-iterable-to-readable-stream
API
Table of Contents
index
Convert an async iterable into a readable stream
Parameters
Returns any Readable<Buffer | string>