@@ -1918,7 +1918,7 @@ import { Resolver } from 'dns/promises';
1918
1918
await Readable .from ([1 , 2 , 3 , 4 ]).toArray (); // [1, 2, 3, 4]
1919
1919
1920
1920
// Make dns queries concurrently using .map and collect
1921
- // the results into an aray using toArray
1921
+ // the results into an array using toArray
1922
1922
const dnsResults = await Readable .from ([
1923
1923
' nodejs.org' ,
1924
1924
' openjsf.org' ,
@@ -1929,6 +1929,102 @@ const dnsResults = await Readable.from([
1929
1929
}, { concurrency: 2 }).toArray ();
1930
1930
```
1931
1931
1932
+ ### ` readable.some(fn[, options]) `
1933
+
1934
+ <!-- YAML
1935
+ added: REPLACEME
1936
+ -->
1937
+
1938
+ > Stability: 1 - Experimental
1939
+
1940
+ * ` fn ` {Function|AsyncFunction} a function to call on each item of the stream.
1941
+ * ` data ` {any} a chunk of data from the stream.
1942
+ * ` options ` {Object}
1943
+ * ` signal ` {AbortSignal} aborted if the stream is destroyed allowing to
1944
+ abort the ` fn ` call early.
1945
+ * ` options ` {Object}
1946
+ * ` concurrency ` {number} the maximum concurrent invocation of ` fn ` to call
1947
+ on the stream at once. ** Default:** ` 1 ` .
1948
+ * ` signal ` {AbortSignal} allows destroying the stream if the signal is
1949
+ aborted.
1950
+ * Returns: {Promise} a promise evaluating to ` true ` if ` fn ` returned a truthy
1951
+ value for some of the chunks.
1952
+
1953
+ This method is similar to ` Array.prototype.some ` and calls ` fn ` on each chunk
1954
+ in the stream until one item returns true (or any truthy value). Once an ` fn `
1955
+ call on a chunk returns a truthy value the stream is destroyed and the promise
1956
+ is fulfilled with ` true ` . If none of the ` fn ` calls on the chunks return a
1957
+ truthy value the promise is fulfilled with ` false ` .
1958
+
1959
+ ``` mjs
1960
+ import { Readable } from ' stream' ;
1961
+ import { stat } from ' fs/promises' ;
1962
+
1963
+ // With a synchronous predicate.
1964
+ await Readable .from ([1 , 2 , 3 , 4 ]).some ((x ) => x > 2 ); // true
1965
+ await Readable .from ([1 , 2 , 3 , 4 ]).some ((x ) => x < 0 ); // false
1966
+
1967
+ // With an asynchronous predicate, making at most 2 file checks at a time.
1968
+ const anyBigFile = await Readable .from ([
1969
+ ' file1' ,
1970
+ ' file2' ,
1971
+ ' file3' ,
1972
+ ]).some (async (fileName ) => {
1973
+ const stats = await stat (fileName);
1974
+ return stat .size > 1024 * 1024 ;
1975
+ }, { concurrency: 2 });
1976
+ console .log (anyBigFile); // `true` if any file in the list is bigger than 1MB
1977
+ console .log (' done' ); // Stream has finished
1978
+ ```
1979
+
1980
+ ### ` readable.every(fn[, options]) `
1981
+
1982
+ <!-- YAML
1983
+ added: REPLACEME
1984
+ -->
1985
+
1986
+ > Stability: 1 - Experimental
1987
+
1988
+ * ` fn ` {Function|AsyncFunction} a function to call on each item of the stream.
1989
+ * ` data ` {any} a chunk of data from the stream.
1990
+ * ` options ` {Object}
1991
+ * ` signal ` {AbortSignal} aborted if the stream is destroyed allowing to
1992
+ abort the ` fn ` call early.
1993
+ * ` options ` {Object}
1994
+ * ` concurrency ` {number} the maximum concurrent invocation of ` fn ` to call
1995
+ on the stream at once. ** Default:** ` 1 ` .
1996
+ * ` signal ` {AbortSignal} allows destroying the stream if the signal is
1997
+ aborted.
1998
+ * Returns: {Promise} a promise evaluating to ` true ` if ` fn ` returned a truthy
1999
+ value for all of the chunks.
2000
+
2001
+ This method is similar to ` Array.prototype.every ` and calls ` fn ` on each chunk
2002
+ in the stream to check if they all return a truthy value for ` fn ` . Once an ` fn `
2003
+ call on a chunk returns a falsy value the stream is destroyed and the promise
2004
+ is fulfilled with ` false ` . If all of the ` fn ` calls on the chunks return a
2005
+ truthy value the promise is fulfilled with ` true ` .
2006
+
2007
+ ``` mjs
2008
+ import { Readable } from ' stream' ;
2009
+ import { stat } from ' fs/promises' ;
2010
+
2011
+ // With a synchronous predicate.
2012
+ await Readable .from ([1 , 2 , 3 , 4 ]).every ((x ) => x > 2 ); // false
2013
+ await Readable .from ([1 , 2 , 3 , 4 ]).every ((x ) => x > 0 ); // true
2014
+
2015
+ // With an asynchronous predicate, making at most 2 file checks at a time.
2016
+ const allBigFiles = await Readable .from ([
2017
+ ' file1' ,
2018
+ ' file2' ,
2019
+ ' file3' ,
2020
+ ]).every (async (fileName ) => {
2021
+ const stats = await stat (fileName);
2022
+ return stat .size > 1024 * 1024 ;
2023
+ }, { concurrency: 2 });
2024
+ console .log (anyBigFile); // `true` if all files in the list are bigger than 1MiB
2025
+ console .log (' done' ); // Stream has finished
2026
+ ```
2027
+
1932
2028
### Duplex and transform streams
1933
2029
1934
2030
#### Class: ` stream.Duplex `
0 commit comments