readable.flatMap(fn[, options])


新增于: v17.5.0, v16.15.0
稳定性: 1 - 实验性的

\Stability: 1 - Experimental

此方法通过将给定回调应用于流的每个块然后展平结果来返回新流。

\This method returns a new stream by applying the given callback to each chunk of the stream and then flattening the result.

可以从 fn 返回流或另一个可迭代或异步可迭代,结果流将合并(展平)到返回的流中。

\It is possible to return a stream or another iterable or async iterable from fn and the result streams will be merged (flattened) into the returned stream.

import { Readable } from 'node:stream';
import { createReadStream } from 'node:fs';
// With a synchronous mapper.
for await (const chunk of Readable.from([1, 2, 3, 4]).flatMap((x) => [x, x])) {
 console.log(chunk); // 1, 1, 2, 2, 3, 3, 4, 4
}
// With an asynchronous mapper, combine the contents of 4 files
const concatResult = Readable.from([
 './1.mjs',
 './2.mjs',
 './3.mjs',
 './4.mjs',
]).flatMap((fileName) => createReadStream(fileName));
for await (const result of concatResult) {
 // This will contain the contents (all chunks) of all 4 files
 console.log(result);
} 

AltStyle によって変換されたページ (->オリジナル) /