-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
41 lines (35 loc) · 907 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
'use strict';
var Readable = require('stream').Readable;
var inherits = require('util').inherits;
function StreamFromPromise(promise, options) {
if (!(this instanceof StreamFromPromise)) {
return new StreamFromPromise(promise, options);
}
Readable.call(this, options);
this.__promise = promise;
this.__resolvingPromise = false;
}
inherits(StreamFromPromise, Readable);
StreamFromPromise.obj = function (promise, options) {
options = options || {};
options.objectMode = true;
return new StreamFromPromise(promise, options);
};
StreamFromPromise.prototype._read = function () {
var self;
if (!this.__resolvingPromise) {
this.__resolvingPromise = true;
self = this;
this.__promise.then(
function (value) {
self.push(value);
self.push(null);
},
function (reason) {
self.emit('error', reason);
self.push(null);
}
);
}
};
module.exports = StreamFromPromise;