stream.Readable.from(iterable, [options])


  • iterable <Iterable> 实现 Symbol.asyncIteratorSymbol.iterator 可迭代协议的对象。
  • options <Object> 提供给 new stream.Readable([options]) 的选项。 默认情况下, Readable.from() 会将 options.objectMode 设置为 true,除非通过将 options.objectMode 设置为 false 显式地选择此选项。
  • 返回: <stream.Readable>

一个从迭代器中创建可读流的实用方法。

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

async function * generate() {
  yield 'hello';
  yield 'streams';
}

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

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

出于性能原因,调用 Readable.from(string)Readable.from(buffer) 将不会迭代字符串或 buffer 以匹配其他流的语义。

  • iterable <Iterable> Object implementing the Symbol.asyncIterator or Symbol.iterator iterable protocol.
  • options <Object> Options provided to new stream.Readable([options]). By default, Readable.from() will set options.objectMode to true, unless this is explicitly opted out by setting options.objectMode to false.
  • Returns: <stream.Readable>

A utility method for creating Readable Streams out of iterators.

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

async function * generate() {
  yield 'hello';
  yield 'streams';
}

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

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

Calling Readable.from(string) or Readable.from(buffer) will not have the strings or buffers be iterated to match the other streams semantics for performance reasons.