Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
853 views
in Technique[技术] by (71.8m points)

angular - How to use exhaustMap in ReactiveX/rxjs 5 in TypeScript

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...