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


  • iterable <Iterable> 实现 Symbol.asyncIteratorSymbol.iterator 可迭代协议的对象。 如果传入空值,则触发 'error' 事件。
  • 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) 不会迭代字符串或缓冲区以匹配其他流语义。

  • iterable <Iterable> Object implementing the Symbol.asyncIterator or Symbol.iterator iterable protocol. Emits an 'error' event if a null value is passed.
  • 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.