当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - 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);

});

});

网友答案:

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

分享给朋友:
您可能感兴趣的文章:
随机阅读: