Skip to content

Instantly share code, notes, and snippets.

@Willmo36
Last active July 21, 2016 19:03
Show Gist options
  • Save Willmo36/9a7d5a76c1bea5391a88 to your computer and use it in GitHub Desktop.
Save Willmo36/9a7d5a76c1bea5391a88 to your computer and use it in GitHub Desktop.
RxJS stream from SuperAgent request
let Rx = require("rx");
Rx.Observable.fromSuperagent = request => () => Rx.Observable.create(observer => {
request.end((err, res) => {
if (err) {
observer.onError(err)
} else {
observer.onNext(res);
}
observer.onCompleted();
})
});
Rx.Observable.fromSuperagent = function(req){
return Rx.Observable.create(function(observable){
req.end(function(err, res){
if(err){
observable.onError(err);
} else {
observable.onNext(res);
}
observable.onCompleted();
})
});
};
@Willmo36
Copy link
Author

Usage example:

let responseStream = requestStream.map(url => {
        return Rx.Observable.fromSuperagent(request.get(url));
}).concatAll();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment