Stream.toStreamable
- 值:
Symbol.for('Stream.toStreamable')
这个值必须是一个函数,能同步地将对象转换成可流式处理的值。当对象被传给 fromSync() 时,会调用这个方法来生成实际的数据。它必须同步返回一个可流式处理的值:一个字符串、Uint8Array 或 Iterable。
🌐 The value must be a function that synchronously converts the object into a
streamable value. When the object is passed to fromSync(), this method is
called to produce the actual data. It must synchronously return a streamable
value: a string, Uint8Array, or Iterable.
import { fromSync, textSync } from 'node:stream/iter';
class Greeting {
#name;
constructor(name) {
this.#name = name;
}
[Symbol.for('Stream.toStreamable')]() {
return `hello ${this.#name}`;
}
}
const stream = fromSync(new Greeting('world'));
console.log(textSync(stream)); // 'hello world'const { fromSync, textSync } = require('node:stream/iter');
class Greeting {
#name;
constructor(name) {
this.#name = name;
}
[Symbol.for('Stream.toStreamable')]() {
return `hello ${this.#name}`;
}
}
const stream = fromSync(new Greeting('world'));
console.log(textSync(stream)); // 'hello world'