@@ -1745,7 +1745,8 @@ added: v17.4.0
1745
1745
1746
1746
> Stability: 1 - Experimental
1747
1747
1748
- * ` fn ` {Function|AsyncFunction} a function to map over every item in the stream.
1748
+ * ` fn ` {Function|AsyncFunction} a function to map over every chunk in the
1749
+ stream.
1749
1750
* ` data ` {any} a chunk of data from the stream.
1750
1751
* ` options ` {Object}
1751
1752
* ` signal ` {AbortSignal} aborted if the stream is destroyed allowing to
@@ -1758,16 +1759,16 @@ added: v17.4.0
1758
1759
* Returns: {Readable} a stream mapped with the function ` fn ` .
1759
1760
1760
1761
This method allows mapping over the stream. The ` fn ` function will be called
1761
- for every item in the stream. If the ` fn ` function returns a promise - that
1762
+ for every chunk in the stream. If the ` fn ` function returns a promise - that
1762
1763
promise will be ` await ` ed before being passed to the result stream.
1763
1764
1764
1765
``` mjs
1765
1766
import { Readable } from ' stream' ;
1766
1767
import { Resolver } from ' dns/promises' ;
1767
1768
1768
1769
// With a synchronous mapper.
1769
- for await (const item of Readable .from ([1 , 2 , 3 , 4 ]).map ((x ) => x * 2 )) {
1770
- console .log (item ); // 2, 4, 6, 8
1770
+ for await (const chunk of Readable .from ([1 , 2 , 3 , 4 ]).map ((x ) => x * 2 )) {
1771
+ console .log (chunk ); // 2, 4, 6, 8
1771
1772
}
1772
1773
// With an asynchronous mapper, making at most 2 queries at a time.
1773
1774
const resolver = new Resolver ();
@@ -1789,7 +1790,7 @@ added: v17.4.0
1789
1790
1790
1791
> Stability: 1 - Experimental
1791
1792
1792
- * ` fn ` {Function|AsyncFunction} a function to filter items from stream.
1793
+ * ` fn ` {Function|AsyncFunction} a function to filter chunks from the stream.
1793
1794
* ` data ` {any} a chunk of data from the stream.
1794
1795
* ` options ` {Object}
1795
1796
* ` signal ` {AbortSignal} aborted if the stream is destroyed allowing to
@@ -1801,8 +1802,8 @@ added: v17.4.0
1801
1802
aborted.
1802
1803
* Returns: {Readable} a stream filtered with the predicate ` fn ` .
1803
1804
1804
- This method allows filtering the stream. For each item in the stream the ` fn `
1805
- function will be called and if it returns a truthy value, the item will be
1805
+ This method allows filtering the stream. For each chunk in the stream the ` fn `
1806
+ function will be called and if it returns a truthy value, the chunk will be
1806
1807
passed to the result stream. If the ` fn ` function returns a promise - that
1807
1808
promise will be ` await ` ed.
1808
1809
@@ -1811,8 +1812,8 @@ import { Readable } from 'stream';
1811
1812
import { Resolver } from ' dns/promises' ;
1812
1813
1813
1814
// With a synchronous predicate.
1814
- for await (const item of Readable .from ([1 , 2 , 3 , 4 ]).filter ((x ) => x > 2 )) {
1815
- console .log (item ); // 3, 4
1815
+ for await (const chunk of Readable .from ([1 , 2 , 3 , 4 ]).filter ((x ) => x > 2 )) {
1816
+ console .log (chunk ); // 3, 4
1816
1817
}
1817
1818
// With an asynchronous predicate, making at most 2 queries at a time.
1818
1819
const resolver = new Resolver ();
@@ -1838,7 +1839,7 @@ added: REPLACEME
1838
1839
1839
1840
> Stability: 1 - Experimental
1840
1841
1841
- * ` fn ` {Function|AsyncFunction} a function to call on each item of the stream.
1842
+ * ` fn ` {Function|AsyncFunction} a function to call on each chunk of the stream.
1842
1843
* ` data ` {any} a chunk of data from the stream.
1843
1844
* ` options ` {Object}
1844
1845
* ` signal ` {AbortSignal} aborted if the stream is destroyed allowing to
@@ -1850,12 +1851,12 @@ added: REPLACEME
1850
1851
aborted.
1851
1852
* Returns: {Promise} a promise for when the stream has finished.
1852
1853
1853
- This method allows iterating a stream. For each item in the stream the
1854
+ This method allows iterating a stream. For each chunk in the stream the
1854
1855
` fn ` function will be called. If the ` fn ` function returns a promise - that
1855
1856
promise will be ` await ` ed.
1856
1857
1857
1858
This method is different from ` for await...of ` loops in that it can optionally
1858
- process items concurrently. In addition, a ` forEach ` iteration can only be
1859
+ process chunks concurrently. In addition, a ` forEach ` iteration can only be
1859
1860
stopped by having passed a ` signal ` option and aborting the related
1860
1861
` AbortController ` while ` for await...of ` can be stopped with ` break ` or
1861
1862
` return ` . In either case the stream will be destroyed.
@@ -1869,8 +1870,8 @@ import { Readable } from 'stream';
1869
1870
import { Resolver } from ' dns/promises' ;
1870
1871
1871
1872
// With a synchronous predicate.
1872
- for await (const item of Readable .from ([1 , 2 , 3 , 4 ]).filter ((x ) => x > 2 )) {
1873
- console .log (item ); // 3, 4
1873
+ for await (const chunk of Readable .from ([1 , 2 , 3 , 4 ]).filter ((x ) => x > 2 )) {
1874
+ console .log (chunk ); // 3, 4
1874
1875
}
1875
1876
// With an asynchronous predicate, making at most 2 queries at a time.
1876
1877
const resolver = new Resolver ();
@@ -1935,7 +1936,7 @@ added: REPLACEME
1935
1936
1936
1937
> Stability: 1 - Experimental
1937
1938
1938
- * ` fn ` {Function|AsyncFunction} a function to call on each item of the stream.
1939
+ * ` fn ` {Function|AsyncFunction} a function to call on each chunk of the stream.
1939
1940
* ` data ` {any} a chunk of data from the stream.
1940
1941
* ` options ` {Object}
1941
1942
* ` signal ` {AbortSignal} aborted if the stream is destroyed allowing to
@@ -1976,6 +1977,56 @@ console.log(anyBigFile); // `true` if any file in the list is bigger than 1MB
1976
1977
console .log (' done' ); // Stream has finished
1977
1978
```
1978
1979
1980
+ ### ` readable.find(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 chunk 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 the first chunk for which ` fn `
1999
+ evaluated with a truthy value, or ` undefined ` if no element was found.
2000
+
2001
+ This method is similar to ` Array.prototype.find ` and calls ` fn ` on each chunk
2002
+ in the stream to find a chunk with a truthy value for ` fn ` . Once an ` fn ` call's
2003
+ awaited return value is truthy, the stream is destroyed and the promise is
2004
+ fulfilled with value for which ` fn ` returned a truthy value. If all of the
2005
+ ` fn ` calls on the chunks return a falsy value, the promise is fulfilled with
2006
+ ` undefined ` .
2007
+
2008
+ ``` mjs
2009
+ import { Readable } from ' stream' ;
2010
+ import { stat } from ' fs/promises' ;
2011
+
2012
+ // With a synchronous predicate.
2013
+ await Readable .from ([1 , 2 , 3 , 4 ]).find ((x ) => x > 2 ); // 3
2014
+ await Readable .from ([1 , 2 , 3 , 4 ]).find ((x ) => x > 0 ); // 1
2015
+ await Readable .from ([1 , 2 , 3 , 4 ]).find ((x ) => x > 10 ); // undefined
2016
+
2017
+ // With an asynchronous predicate, making at most 2 file checks at a time.
2018
+ const foundBigFile = await Readable .from ([
2019
+ ' file1' ,
2020
+ ' file2' ,
2021
+ ' file3' ,
2022
+ ]).find (async (fileName ) => {
2023
+ const stats = await stat (fileName);
2024
+ return stat .size > 1024 * 1024 ;
2025
+ }, { concurrency: 2 });
2026
+ console .log (foundBigFile); // File name of large file, if any file in the list is bigger than 1MB
2027
+ console .log (' done' ); // Stream has finished
2028
+ ```
2029
+
1979
2030
### ` readable.every(fn[, options]) `
1980
2031
1981
2032
<!-- YAML
@@ -1984,7 +2035,7 @@ added: REPLACEME
1984
2035
1985
2036
> Stability: 1 - Experimental
1986
2037
1987
- * ` fn ` {Function|AsyncFunction} a function to call on each item of the stream.
2038
+ * ` fn ` {Function|AsyncFunction} a function to call on each chunk of the stream.
1988
2039
* ` data ` {any} a chunk of data from the stream.
1989
2040
* ` options ` {Object}
1990
2041
* ` signal ` {AbortSignal} aborted if the stream is destroyed allowing to
@@ -2034,7 +2085,7 @@ added: REPLACEME
2034
2085
> Stability: 1 - Experimental
2035
2086
2036
2087
* ` fn ` {Function|AsyncGeneratorFunction|AsyncFunction} a function to map over
2037
- every item in the stream.
2088
+ every chunk in the stream.
2038
2089
* ` data ` {any} a chunk of data from the stream.
2039
2090
* ` options ` {Object}
2040
2091
* ` signal ` {AbortSignal} aborted if the stream is destroyed allowing to
@@ -2058,8 +2109,8 @@ import { Readable } from 'stream';
2058
2109
import { createReadStream } from ' fs' ;
2059
2110
2060
2111
// With a synchronous mapper.
2061
- for await (const item of Readable .from ([1 , 2 , 3 , 4 ]).flatMap ((x ) => [x, x])) {
2062
- console .log (item ); // 1, 1, 2, 2, 3, 3, 4, 4
2112
+ for await (const chunk of Readable .from ([1 , 2 , 3 , 4 ]).flatMap ((x ) => [x, x])) {
2113
+ console .log (chunk ); // 1, 1, 2, 2, 3, 3, 4, 4
2063
2114
}
2064
2115
// With an asynchronous mapper, combine the contents of 4 files
2065
2116
const concatResult = Readable .from ([
0 commit comments