11# Stream To Async Iterator [ ![ npm version] ( https://badge.fury.io/js/stream-to-async-iterator.svg )] ( https://www.npmjs.com/package/stream-to-async-iterator ) [ ![ Build Status] ( https://travis-ci.org/basicdays/node-stream-to-async-iterator.svg?branch=master )] ( https://travis-ci.org/basicdays/node-stream-to-async-iterator )
22
3-
43## Overview
54
65` stream-to-async-iterator ` provides a wrapper that implements ` Symbol.asyncIterator ` . This will allow streams to be
76usable as async iterables that can be used in for-await-of loops.
87
9- Supports node.js 6 and up.
8+ Supports node.js 12 and up.
109
1110## Installation
1211
12+ With NPM:
13+
14+ ``` shell
15+ npm install stream-to-async-iterator
1316```
14- $ npm install stream-to-async-iterator
17+
18+ With Yarn:
19+
20+ ``` shell
21+ yarn add stream-to-async-iterator
1522```
1623
1724The included examples use async/await syntax for for-of loops. This assumes you are in an environment that natively
1825supports this new syntax, or that you use a tool such as Babel. In addition, for async iterators to work properly,
19- the ` Symbol.asyncIterator ` symbol must be defined. Core-js or ` babel-polyfill ` can both help with that.
20-
26+ the ` Symbol.asyncIterator ` symbol must be defined. Core-js can help with that.
2127
2228## Usage
2329
@@ -30,22 +36,27 @@ information.
3036
3137``` js
3238#! / usr/ bin/ env node
33- ' use strict' ;
34- require (' core-js/es7/symbol' );
35- const fs = require (' fs' );
36- const S2A = require (' stream-to-async-iterator' );
39+ " use strict" ;
40+ const { Readable } = require (" stream" );
41+ const S2A = require (" ../" ).default ;
3742
38-
39- (async function () {
40- const readStream = fs .createReadStream (__filename );
43+ (async function () {
44+ const readStream = Readable .from ([1 , 2 , 3 ]);
4145 for await (const chunk of new S2A (readStream)) {
42- console .log ( chunk);
46+ console .dir ({ chunk } );
4347 }
4448})();
4549```
4650
51+ Outputs:
52+
53+ ```
54+ { chunk: 1 }
55+ { chunk: 2 }
56+ { chunk: 3 }
57+ ```
4758
4859## References
4960
50- - https://github.com/tc39/proposal-async-iteration
51- - https://github.com/babel/babel/tree/master/packages/babel-plugin-transform-async-generator-functions
61+ - https://github.com/tc39/proposal-async-iteration
62+ - https://github.com/babel/babel/tree/master/packages/babel-plugin-transform-async-generator-functions
0 commit comments