1
1
# 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 )
2
2
3
-
4
3
## Overview
5
4
6
5
` stream-to-async-iterator ` provides a wrapper that implements ` Symbol.asyncIterator ` . This will allow streams to be
7
6
usable as async iterables that can be used in for-await-of loops.
8
7
9
- Supports node.js 6 and up.
8
+ Supports node.js 12 and up.
10
9
11
10
## Installation
12
11
12
+ With NPM:
13
+
14
+ ``` shell
15
+ npm install stream-to-async-iterator
13
16
```
14
- $ npm install stream-to-async-iterator
17
+
18
+ With Yarn:
19
+
20
+ ``` shell
21
+ yarn add stream-to-async-iterator
15
22
```
16
23
17
24
The included examples use async/await syntax for for-of loops. This assumes you are in an environment that natively
18
25
supports 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.
21
27
22
28
## Usage
23
29
@@ -30,22 +36,27 @@ information.
30
36
31
37
``` js
32
38
#! / 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 ;
37
42
38
-
39
- (async function () {
40
- const readStream = fs .createReadStream (__filename );
43
+ (async function () {
44
+ const readStream = Readable .from ([1 , 2 , 3 ]);
41
45
for await (const chunk of new S2A (readStream)) {
42
- console .log ( chunk);
46
+ console .dir ({ chunk } );
43
47
}
44
48
})();
45
49
```
46
50
51
+ Outputs:
52
+
53
+ ```
54
+ { chunk: 1 }
55
+ { chunk: 2 }
56
+ { chunk: 3 }
57
+ ```
47
58
48
59
## References
49
60
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