share(source[, options])
source<AsyncIterable> 共享的来源。options<Object>- 返回:Share
创建一个拉取模式的多消费者共享流。与 broadcast() 不同,源只有在消费者拉取时才会被读取。多个消费者共享一个缓冲区。
🌐 Create a pull-model multi-consumer shared stream. Unlike broadcast(), the
source is only read when a consumer pulls. Multiple consumers share a single
buffer.
import { from, share, text } from 'node:stream/iter';
const shared = share(from('hello'));
const c1 = shared.pull();
const c2 = shared.pull();
// Consume concurrently to avoid deadlock with small buffers.
const [r1, r2] = await Promise.all([text(c1), text(c2)]);
console.log(r1); // 'hello'
console.log(r2); // 'hello'const { from, share, text } = require('node:stream/iter');
async function run() {
const shared = share(from('hello'));
const c1 = shared.pull();
const c2 = shared.pull();
// Consume concurrently to avoid deadlock with small buffers.
const [r1, r2] = await Promise.all([text(c1), text(c2)]);
console.log(r1); // 'hello'
console.log(r2); // 'hello'
}
run().catch(console.error);