How to use exhaustMap in ReactiveX/rxjs 5 in TypeScript

杀马特。学长 韩版系。学妹 提交于 2019-11-27 06:15:28

问题


I was wondering how can I process an array where each value returns a Promise in the same order as they're specified. For example, let's say I want to call multiple Ajax calls in this order:

var array = [
    'http://example.org',
    'http://otherexample.org',
    'http://anotherexample.org',
];

There's basicaly the same question: How can I use RxJs to hold off any requests for an AJAX call until the previous one resolves, which suggests using flatMapFirst.

Since I'm using Angular2 (beta-15 at this moment) in TypeScript which uses rxjs@5.0.0-beta.2 I found out that flatMapFirst has been renamed to exhaustMap.

But this operator isn't listed in Observable.ts, therefore I can't use it. It's not even present in the bundled version of RxJS https://code.angularjs.org/2.0.0-beta.15/Rx.js.

So, how am I supposed to use it? Or is there any other way to the what I need? Its package.json lists a lot of build scripts, should I force it to use one of them?

Edit: I should mention I'm using ReactiveX/rxjs library, not Reactive-Extensions/RxJS


回答1:


Operator concatMap() did the job:

import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/concatMap';

var urls = [
  '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',
];     

Observable.from(this.urls)
  .concatMap(url => http.get(url))
  .subscribe(response => console.log(response.url))
;

This prints to console:

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


来源:https://stackoverflow.com/questions/36713531/how-to-use-exhaustmap-in-reactivex-rxjs-5-in-typescript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!