readable.toArray([options])
稳定性: 1 - 实验性的
¥Stability: 1 - Experimental
-
options
<Object>-
signal
<AbortSignal> 如果信号中止,则允许取消 toArray 操作。¥
signal
<AbortSignal> allows cancelling the toArray operation if the signal is aborted.
-
-
返回:<Promise> 一个包含包含流内容的数组的 Promise。
¥Returns: <Promise> a promise containing an array with the contents of the stream.
此方法允许轻松获取流的内容。
¥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();