如何在TypeScript的ReactiveX/rxjs 5中使用exhaustMap [英] How to use exhaustMap in ReactiveX/rxjs 5 in TypeScript

查看:64
本文介绍了如何在TypeScript的ReactiveX/rxjs 5中使用exhaustMap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何处理一个数组,其中每个值以指定的顺序返回Promise.例如,假设我要以此顺序调用多个Ajax调用:

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.

由于我在使用rxjs@5.0.0-beta.2的TypeScript中使用Angular2(目前为beta-15),所以我发现

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.

但是 Observable.ts ,因此我无法使用它. RxJS https://code的捆绑版本中甚至都不存在. angularjs.org/2.0.0-beta.15/Rx.js .

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.

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

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?

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

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

推荐答案

操作员concatMap() 完成了该工作:

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))
;

这将打印到控制台:

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

这篇关于如何在TypeScript的ReactiveX/rxjs 5中使用exhaustMap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆