Madhu
Madhu

Reputation: 121

Combine two ajax response in RXJS

I am still learning RXJS(just started), I know my question would be of starters. I am trying to combine two responses from external call. Say I make call1 and call2 and I should be able to subscribe with one combined response.

Below is my code

var arrayWeather =['http://apidev.accuweather.com/currentconditions/v1/250111.json?language=en&apikey=hoArfRosT1215', 'http://apidev.accuweather.com/currentconditions/v1/4-204108_1_AL.json?language=en&apikey=hoArfRosT1215'];
var refreshClickStream = Rx.Observable.fromEvent(document.querySelector('.refresh'), 'click');

refreshClickStream.subscribe(function(e){
  Rx.Observable.fromArray(arrayWeather)
    .flatMap(function(datathings){ 
        return Rx.DOM.Request.get(datathings)
    })
    .subscribe(function(data){
        console.log(data.response);
    });
});

Upvotes: 2

Views: 1000

Answers (1)

fahadash
fahadash

Reputation: 3281

You need a Zip

var combined = Rx.Observable.zip(refreshClickStream, anotherStream);

Then subscribe to the combined

var subscription = combined.subscribe(
  function (x) {
  },
  function (err) {
  },
  function () {
  });

You can also add a function in Zip where you would tell zip operation how to combine the two

var combined = Rx.Observable.zip(refreshClickStream, anotherStream, function (firstStreamItem, secondStreamItem) {
    return { first: firstStreamItem, second: secondStreamItem };
  });

For more information about RxJS Zip, see here

Upvotes: 1

Related Questions