Stream.toAsyncStreamable
- 值:
Symbol.for('Stream.toAsyncStreamable')
该值必须是一个将对象转换为可流式传输值的函数。当对象在流式处理管道中的任何位置被遇到时(作为传递给 from() 的源,或作为从转换中返回的值),将调用此方法以生成实际数据。它可以返回(或解析为)任何可流式传输的值:字符串、Uint8Array、AsyncIterable、Iterable 或另一个可流式传输对象。
🌐 The value must be a function that converts the object into a streamable value.
When the object is encountered anywhere in the streaming pipeline (as a source
passed to from(), or as a value returned from a transform), this method is
called to produce the actual data. It may return (or resolve to) any streamable
value: a string, Uint8Array, AsyncIterable, Iterable, or another streamable
object.
import { from, text } from 'node:stream/iter';
class Greeting {
#name;
constructor(name) {
this.#name = name;
}
[Symbol.for('Stream.toAsyncStreamable')]() {
return `hello ${this.#name}`;
}
}
const stream = from(new Greeting('world'));
console.log(await text(stream)); // 'hello world'const { from, text } = require('node:stream/iter');
class Greeting {
#name;
constructor(name) {
this.#name = name;
}
[Symbol.for('Stream.toAsyncStreamable')]() {
return `hello ${this.#name}`;
}
}
const stream = from(new Greeting('world'));
text(stream).then(console.log); // 'hello world'