5

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

reduckted
  • 2,358
  • 3
  • 28
  • 37
martin
  • 93,354
  • 25
  • 191
  • 226
  • 3
    Based on your problem description, you may actually want to use `concatMap` instead. It sounds like you want to process all requests without throwing any away. – Brandon Apr 19 '16 at 13:46
  • 1
    You're right `concatMap` did what I need. – martin Apr 20 '16 at 07:31

1 Answers1

5

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
martin
  • 93,354
  • 25
  • 191
  • 226