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 passed to from(), this method is called to produce the actual data. It may return any value that resolves to 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'