@@ -1863,7 +1863,7 @@ import { Resolver } from 'dns/promises';
1863
1863
await Readable .from ([1 , 2 , 3 , 4 ]).toArray (); // [1, 2, 3, 4]
1864
1864
1865
1865
// Make dns queries concurrently using .map and collect
1866
- // the results into an aray using toArray
1866
+ // the results into an array using toArray
1867
1867
const dnsResults = await Readable .from ([
1868
1868
' nodejs.org' ,
1869
1869
' openjsf.org' ,
@@ -1874,6 +1874,104 @@ const dnsResults = await Readable.from([
1874
1874
}, { concurrency: 2 }).toArray ();
1875
1875
```
1876
1876
1877
+ ### ` readable.some(fn[, options]) `
1878
+
1879
+ <!-- YAML
1880
+ added: REPLACEME
1881
+ -->
1882
+
1883
+ > Stability: 1 - Experimental
1884
+
1885
+ * ` fn ` {Function|AsyncFunction} a function to call on each item of the stream.
1886
+ * ` data ` {any} a chunk of data from the stream.
1887
+ * ` options ` {Object}
1888
+ * ` signal ` {AbortSignal} aborted if the stream is destroyed allowing to
1889
+ abort the ` fn ` call early.
1890
+ * ` options ` {Object}
1891
+ * ` concurrency ` {number} the maximum concurrent invocation of ` fn ` to call
1892
+ on the stream at once. ** Default:** ` 1 ` .
1893
+ * ` signal ` {AbortSignal} allows destroying the stream if the signal is
1894
+ aborted.
1895
+ * Returns: {Promise} a promise evaluating to ` true ` if ` fn ` returned a truthy
1896
+ value for at least one of the chunks.
1897
+
1898
+ This method is similar to ` Array.prototype.some ` and calls ` fn ` on each chunk
1899
+ in the stream until the awaited return value is ` true ` (or any truthy value).
1900
+ Once an ` fn ` call on a chunk awaited return value is truthy, the stream is
1901
+ destroyed and the promise is fulfilled with ` true ` . If none of the ` fn `
1902
+ calls on the chunks return a truthy value, the promise is fulfilled with
1903
+ ` false ` .
1904
+
1905
+ ``` mjs
1906
+ import { Readable } from ' stream' ;
1907
+ import { stat } from ' fs/promises' ;
1908
+
1909
+ // With a synchronous predicate.
1910
+ await Readable .from ([1 , 2 , 3 , 4 ]).some ((x ) => x > 2 ); // true
1911
+ await Readable .from ([1 , 2 , 3 , 4 ]).some ((x ) => x < 0 ); // false
1912
+
1913
+ // With an asynchronous predicate, making at most 2 file checks at a time.
1914
+ const anyBigFile = await Readable .from ([
1915
+ ' file1' ,
1916
+ ' file2' ,
1917
+ ' file3' ,
1918
+ ]).some (async (fileName ) => {
1919
+ const stats = await stat (fileName);
1920
+ return stat .size > 1024 * 1024 ;
1921
+ }, { concurrency: 2 });
1922
+ console .log (anyBigFile); // `true` if any file in the list is bigger than 1MB
1923
+ console .log (' done' ); // Stream has finished
1924
+ ```
1925
+
1926
+ ### ` readable.every(fn[, options]) `
1927
+
1928
+ <!-- YAML
1929
+ added: REPLACEME
1930
+ -->
1931
+
1932
+ > Stability: 1 - Experimental
1933
+
1934
+ * ` fn ` {Function|AsyncFunction} a function to call on each item of the stream.
1935
+ * ` data ` {any} a chunk of data from the stream.
1936
+ * ` options ` {Object}
1937
+ * ` signal ` {AbortSignal} aborted if the stream is destroyed allowing to
1938
+ abort the ` fn ` call early.
1939
+ * ` options ` {Object}
1940
+ * ` concurrency ` {number} the maximum concurrent invocation of ` fn ` to call
1941
+ on the stream at once. ** Default:** ` 1 ` .
1942
+ * ` signal ` {AbortSignal} allows destroying the stream if the signal is
1943
+ aborted.
1944
+ * Returns: {Promise} a promise evaluating to ` true ` if ` fn ` returned a truthy
1945
+ value for all of the chunks.
1946
+
1947
+ This method is similar to ` Array.prototype.every ` and calls ` fn ` on each chunk
1948
+ in the stream to check if all awaited return values are truthy value for ` fn ` .
1949
+ Once an ` fn ` call on a chunk awaited return value is falsy, the stream is
1950
+ destroyed and the promise is fulfilled with ` false ` . If all of the ` fn ` calls
1951
+ on the chunks return a truthy value, the promise is fulfilled with ` true ` .
1952
+
1953
+ ``` mjs
1954
+ import { Readable } from ' stream' ;
1955
+ import { stat } from ' fs/promises' ;
1956
+
1957
+ // With a synchronous predicate.
1958
+ await Readable .from ([1 , 2 , 3 , 4 ]).every ((x ) => x > 2 ); // false
1959
+ await Readable .from ([1 , 2 , 3 , 4 ]).every ((x ) => x > 0 ); // true
1960
+
1961
+ // With an asynchronous predicate, making at most 2 file checks at a time.
1962
+ const allBigFiles = await Readable .from ([
1963
+ ' file1' ,
1964
+ ' file2' ,
1965
+ ' file3' ,
1966
+ ]).every (async (fileName ) => {
1967
+ const stats = await stat (fileName);
1968
+ return stat .size > 1024 * 1024 ;
1969
+ }, { concurrency: 2 });
1970
+ // `true` if all files in the list are bigger than 1MiB
1971
+ console .log (allBigFiles);
1972
+ console .log (' done' ); // Stream has finished
1973
+ ```
1974
+
1877
1975
### Duplex and transform streams
1878
1976
1879
1977
#### Class: ` stream.Duplex `
0 commit comments