使用异步生成器创建可读流


可以使用 Readable.from() 实用方法从异步生成器构建 Node.js 可读流:

const { Readable } = require('stream');

async function * generate() {
  yield 'a';
  yield 'b';
  yield 'c';
}

const readable = Readable.from(generate());

readable.on('data', (chunk) => {
  console.log(chunk);
});

We can construct a Node.js readable stream from an asynchronous generator using the Readable.from() utility method:

const { Readable } = require('stream');

async function * generate() {
  yield 'a';
  yield 'b';
  yield 'c';
}

const readable = Readable.from(generate());

readable.on('data', (chunk) => {
  console.log(chunk);
});