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


我们可以使用 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);
});