如何:在rxjs中请求url并将其压缩响应?

时间:2015-07-28 14:12:41

标签: javascript reactive-programming rxjs

我这样做:

let urls = [url1, url2, url3];

let urls_ = Observable
      .from(urls);

let ajax = function() { return promise; };

let results_ = urls_
      .flatMap(ajax)
      .zip(
        urls_,
        (response, url) => {
          return {url, response};
        }
      )

但问题是{response, url}对不匹配,url1并不真正与response1一起使用。我经常面对这个用例而没有操作符,即在初始Observable上做异步,然后向前传递初始的observable和async结果。

RxJS中是否有操作员执行此操作?否则我怎么能在我的例子中完成1-1压缩?

1 个答案:

答案 0 :(得分:3)

flatMap has an overload that will handle it for you, the second argument accepts a method which has the signature

function resultSelector(outerValue, innerValue, outerIndex, innerIndex)

You use it like so

let urls = [url1, url2, url3];

let urls_ = Observable
      .from(urls);

let ajax = function() { return promise; };

let results_ = urls_
      .flatMap(ajax, 
         (url, response) => {
           return {url, response}; 
         })
      )