加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-17 07:56:28 所属栏目:安全 来源:网络整理
导读:我想知道如何处理一个数组,其中每个值按照指定的顺序返回Promise.例如,假设我想按此顺序调用多个Ajax调用: var array = [ 'http://example.org','http://otherexample.org','http://anotherexample.org',]; 基本上是同样的问题:How can I use RxJs to hold
我想知道如何处理一个数组,其中每个值按照指定的顺序返回Promise.例如,假设我想按此顺序调用多个Ajax调用:
var array = [
    '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()完成了这项工作:
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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读