readable.compose(stream[, options])
stream<Writable> | <Duplex> | <WritableStream> | <TransformStream> | <Function>options<Object>signal<AbortSignal> 允许在信号被中止时销毁流。
- 返回:<Duplex> 由流
stream组成的流。
import { Readable } from 'node:stream';
async function* splitToWords(source) {
for await (const chunk of source) {
const words = String(chunk).split(' ');
for (const word of words) {
yield word;
}
}
}
const wordsStream = Readable.from(['text passed through', 'composed stream']).compose(splitToWords);
const words = await wordsStream.toArray();
console.log(words); // prints ['text', 'passed', 'through', 'composed', 'stream'] readable.compose(s) 等同于 stream.compose(readable, s)。
此方法还允许提供一个 <AbortSignal>,当中止时将销毁已组合的流。
🌐 This method also allows for an <AbortSignal> to be provided, which will destroy the composed stream when aborted.
有关更多信息,请参见stream.compose(...streams)。
🌐 See stream.compose(...streams) for more information.