diff options
author | Matteo Collina <hello@matteocollina.com> | 2017-12-19 13:33:31 +0100 |
---|---|---|
committer | Matteo Collina <hello@matteocollina.com> | 2018-01-11 12:34:41 +0100 |
commit | 61b4d60c5d9694e79069b1680b3736c96a5de501 (patch) | |
tree | 1a629098741a9f32da51ad6016eb05af22ad2c8a /doc | |
parent | 4d96c17e054fd4735063dc4aa4341ea76ca12b49 (diff) | |
download | android-node-v8-61b4d60c5d9694e79069b1680b3736c96a5de501.tar.gz android-node-v8-61b4d60c5d9694e79069b1680b3736c96a5de501.tar.bz2 android-node-v8-61b4d60c5d9694e79069b1680b3736c96a5de501.zip |
stream: added experimental support for for-await
Adds support for Symbol.asyncIterator into the Readable class.
The stream is destroyed when the loop terminates with break or throw.
Fixes: https://github.com/nodejs/node/issues/15709
PR-URL: https://github.com/nodejs/node/pull/17755
Fixes: https://github.com/nodejs/node/issues/15709
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com>
Reviewed-By: Michaƫl Zasso <targos@protonmail.com>
Diffstat (limited to 'doc')
-rw-r--r-- | doc/api/stream.md | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/doc/api/stream.md b/doc/api/stream.md index 2166ae4ecc..fe7a9a8f30 100644 --- a/doc/api/stream.md +++ b/doc/api/stream.md @@ -1165,6 +1165,31 @@ readable stream will release any internal resources. Implementors should not override this method, but instead implement [`readable._destroy`][readable-_destroy]. +##### readable[@@asyncIterator] +<!-- YAML +added: REPLACEME +--> + +> Stability: 1 - Experimental + +Returns an [AsyncIterator][async-iterator] to fully consume the stream. + +```js +async function print(readable) { + readable.setEncoding('utf8'); + let data = ''; + for await (const k of readable) { + data += k; + } + console.log(data); +} + +print(fs.createReadStream('file')).catch(console.log); +``` + +If the loop terminates with a `break` or a `throw`, the stream will be destroyed. +In other terms, iterating over a stream will consume the stream fully. + ### Duplex and Transform Streams #### Class: stream.Duplex @@ -2328,3 +2353,4 @@ contain multi-byte characters. [readable-destroy]: #stream_readable_destroy_error [writable-_destroy]: #stream_writable_destroy_err_callback [writable-destroy]: #stream_writable_destroy_error +[async-iterator]: https://github.com/tc39/proposal-async-iteration |