Created
July 29, 2016 01:32
-
-
Save boris317/0a21f326e694d5d893727900c6bbafd0 to your computer and use it in GitHub Desktop.
RXJS Chaining HTTP Calls
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var http = require('http'); | |
var RX = require('rx'); | |
var request = require('request'); | |
function get(url) { | |
return RX.Observable.create(function(observer) { | |
request(url, function(err, response, body) { | |
if (err) { | |
observer.onError(err); | |
return; | |
} | |
observer.onNext(body); | |
observer.onCompleted(); | |
}); | |
}); | |
} | |
var source = get('http://httpbin.org/response-headers?count=0'); | |
source | |
.flatMap(function(body) { | |
var obj = JSON.parse(body); | |
return get('http://httpbin.org/response-headers?count=' + (parseInt(obj.count) + 1)); | |
}) | |
.flatMap(function(body) { | |
var obj = JSON.parse(body); | |
return get('http://httpbin.org/response-headers?count=' + (parseInt(obj.count) + 1)); | |
}) | |
.subscribe(function(body) { console.log(body) }); |
Author
boris317
commented
Jul 29, 2016
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment