readable.toArray([options])
稳定性: 1 - 实验性
options<Object>signal<AbortSignal> 允许在信号被中止时取消 toArray 操作。
- 返回:<Promise> 一个包含流内容数组的 promise。
此方法允许轻松获取流的内容。
【This method allows easily obtaining the contents of a stream.】
由于这种方法会将整个流读入内存,因此抵消了流的优势。它的目的是为了互操作性和方便性,而不是作为使用流的主要方式。
【As this method reads the entire stream into memory, it negates the benefits of streams. It's intended for interoperability and convenience, not as the primary way to consume streams.】
import { Readable } from 'node:stream';
import { Resolver } from 'node:dns/promises';
await Readable.from([1, 2, 3, 4]).toArray(); // [1, 2, 3, 4]
// Make dns queries concurrently using .map and collect
// the results into an array using toArray
const dnsResults = await Readable.from([
'nodejs.org',
'openjsf.org',
'www.linuxfoundation.org',
]).map(async (domain) => {
const { address } = await resolver.resolve4(domain, { ttl: true });
return address;
}, { concurrency: 2 }).toArray();