如何在TypeScript中的ReactiveX / rxjs 5中使用exhaustMap

前端之家收集整理的这篇文章主要介绍了如何在TypeScript中的ReactiveX / rxjs 5中使用exhaustMap前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想知道如何处理一个数组,其中每个值按照指定的顺序返回Promise.例如,假设我想按此顺序调用多个Ajax调用
  1. var array = [
  2. 'http://example.org','http://otherexample.org','http://anotherexample.org',];

基本上是同样的问题:How can I use RxJs to hold off any requests for an AJAX call until the previous one resolves,建议使用flatMapFirst.

由于我在使用rxjs@5.0.0-beta.2的TypeScript中使用Angular2(此刻为beta-15),我发现了flatMapFirst has been renamed to exhaustMap.

但是此运算符未在Observable.ts中列出,因此我无法使用它.它甚至没有出现在捆绑版本的RxJS https://code.angularjs.org/2.0.0-beta.15/Rx.js中.

那么,我该如何使用呢?或者还有其他方法可以满足我的需求吗?它的package.json列出了很多构建脚本,我应该强制它使用其中一个吗?

编辑:我应该提到我使用的是ReactiveX/rxjs库,而不是Reactive-Extensions/RxJS

Operator concatMap()完成了这项工作:
  1. import { Observable } from 'rxjs/Observable';
  2. import 'rxjs/add/operator/concatMap';
  3.  
  4. var urls = [
  5. 'https://httpbin.org/get?1','https://httpbin.org/get?2','https://httpbin.org/get?3','https://httpbin.org/get?4','https://httpbin.org/get?5',];
  6.  
  7. Observable.from(this.urls)
  8. .concatMap(url => http.get(url))
  9. .subscribe(response => console.log(response.url))
  10. ;

这打印到控制台:

  1. https://httpbin.org/get?1
  2. https://httpbin.org/get?2
  3. https://httpbin.org/get?3
  4. https://httpbin.org/get?4
  5. https://httpbin.org/get?5

猜你在找的Angularjs相关文章