Promises and streams using Bluebird.js and Twitter stream

后端 未结 3 2111
走了就别回头了
走了就别回头了 2021-02-09 11:59

I am new super new to Promises and Node and am curious about using promises with streams. Can I promisify a stream? Using Bluebirdjs and the Twit module I have the following:

3条回答
  •  日久生厌
    2021-02-09 12:23

    I ended up using RxJS implementing observables with streams.

    var sanFrancisco = [ '-122.75', '36.8', '-121.75', '37.8' ]
    
    var stream = T.stream('statuses/filter', { locations: sanFrancisco });
    
    var source =  Rx.Node.fromEvent(stream, 'tweet');
    
    var observer = Rx.Observer.create(
        function (tweet) {
            // THIS IS WHERE EACH TWEET SHOULD COME FROM THE STREAM
            console.log(tweet);
        },
        function (err) {
            console.log('Error getting tweets: ' + err);
        },
        function () {
            console.log('Completed');
        }
    );
    
    source.subscribe(observer);
    

    I ended up having to use RX.Observable.fromEvent instead of Rx.Node.fromStream because, the Twit module must handle the actual stream behind the scenes but exposing it through and EventEmitter, they probably shouldn't have named it T.stream.

提交回复
热议问题