readable.find(fn[, options])
添加于:v17.5.0
Stability: 1 - 实验性
参数
fn<Function>|<AsyncFunction>调用流的每个块的函数。data<any> 来自流的数据块。options<Object>signal<AbortSignal> 如果流被破坏则中止,允许提前中止fn调用。
options<Object>concurrency<number>fn的最大并发调用一次调用流。 默认:1。signal<AbortSignal> 允许在信号中止时销毁流。
- 返回: <Promise> 一个对第一个块进行评估的承诺,其中
fn评估为真值,如果没有找到元素,则返回undefined。
此方法类似于 Array.prototype.find 并在流中的每个块上调用 fn 以查找具有 fn 真实值的块。一旦 fn 调用的等待返回值是真实的,流就会被销毁,并且承诺会用 fn 返回真实值的值来实现。如果对块的所有 fn 调用都返回一个虚假值,则使用 undefined 实现承诺。
import { Readable } from 'node:stream';
import { stat } from 'node:fs/promises';
// With a synchronous predicate.
await Readable.from([1, 2, 3, 4]).find((x) => x > 2); // 3
await Readable.from([1, 2, 3, 4]).find((x) => x > 0); // 1
await Readable.from([1, 2, 3, 4]).find((x) => x > 10); // undefined
// With an asynchronous predicate, making at most 2 file checks at a time.
const foundBigFile = await Readable.from([
'file1',
'file2',
'file3',
]).find(async (fileName) => {
const stats = await stat(fileName);
return stat.size > 1024 * 1024;
}, { concurrency: 2 });
console.log(foundBigFile); // File name of large file, if any file in the list is bigger than 1MB
console.log('done'); // Stream has finished
相关用法
- Node.js stream.Readable.filter(fn[, options])用法及代码示例
- Node.js stream.Readable.forEach(fn[, options])用法及代码示例
- Node.js stream.Readable.from()用法及代码示例
- Node.js stream.Readable.flatMap(fn[, options])用法及代码示例
- Node.js stream.Readable.from(iterable[, options])用法及代码示例
- Node.js stream.Readable.take(limit[, options])用法及代码示例
- Node.js stream.Readable.pipe(destination[, options])用法及代码示例
- Node.js stream.Readable.setEncoding(encoding)用法及代码示例
- Node.js stream.Readable.some(fn[, options])用法及代码示例
- Node.js stream.Readable.map(fn[, options])用法及代码示例
- Node.js stream.Readable.toArray([options])用法及代码示例
- Node.js stream.Readable.isPaused()用法及代码示例
- Node.js stream.Readable.every(fn[, options])用法及代码示例
- Node.js stream.Readable.read([size])用法及代码示例
- Node.js stream.Readable.unshift(chunk[, encoding])用法及代码示例
- Node.js stream.Readable.asIndexedPairs([options])用法及代码示例
- Node.js stream.Readable.drop(limit[, options])用法及代码示例
- Node.js stream.Readable.resume()用法及代码示例
- Node.js stream.Readable.reduce(fn[, initial[, options]])用法及代码示例
- Node.js stream.Readable.iterator([options])用法及代码示例
- Node.js stream.Readable.wrap(stream)用法及代码示例
- Node.js stream.Readable.unpipe([destination])用法及代码示例
- Node.js stream.Readable.pause()用法及代码示例
- Node.js stream.Writable.uncork()用法及代码示例
- Node.js stream.finished()用法及代码示例
注:本文由纯净天空筛选整理自nodejs.org大神的英文原创作品 stream.Readable.find(fn[, options])。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
