-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
41 lines (35 loc) · 895 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
const { Readable } = require("stream");
const isAsyncIterator = obj => {
// checks for null and undefined
if (obj == null) {
return false;
}
return typeof obj[Symbol.asyncIterator] === "function";
};
function StreamGenerators(g) {
if (!isAsyncIterator(g))
throw new TypeError("First argument must be a ES6 Async Generator");
Readable.call(this, { objectMode: true });
this._g = g;
}
StreamGenerators.prototype = Object.create(Readable.prototype, {
constructor: { value: StreamGenerators }
});
StreamGenerators.prototype._read = function(size) {
try {
this._g.next().then(r => {
if (false === r.done) {
this.push(r.value);
} else {
this.push(null);
}
}).catch((e)=> {
this.emit("error", e);
});
} catch (e) {
this.emit("error", e);
}
};
module.exports = list => {
return new StreamGenerators(list);
};