switchMap can chain two HTTP requests together, creating one request based on the results of the first request.

But the result observable did not have the data of the first request, instead it only had access to the data of the second HTTP request.

If we would like to have both the data of the first HTTP request and deliver it together with the data of the second request, we could use a selector function (notice the second argument passed to switchMap):

  1. sequentialRequests() {
  2.  
  3. const sequence$ = this.http.get<Course>(
  4. '/courses/-KgVwEBq5wbFnjj7O8Fp.json')
  5. .switchMap(course => {
  6. course.description+= ' - TEST ';
  7. return this.http.put('/courses/-KgVwEBq5wbFnjj7O8Fp.json', course)
  8. },
  9. (firstHTTPResult, secondHTTPResult) =>
  10. [firstHTTPResult, secondHTTPResult]);
  11.  
  12. sequence$.subscribe(
  13. values => console.log("result observable ", values)
  14. );
  15.  
  16. }

Other example:

  1. //emit immediately, then every 5s
  2. const source = Rx.Observable.timer(, );
  3. //switch to new inner observable when source emits, invoke project function and emit values
  4. const example = source.switchMap(() => Rx.Observable.interval(), (outerValue, innerValue, outerIndex, innerIndex) => ({outerValue, innerValue, outerIndex, innerIndex}));
  5. /*
  6. Output:
  7. {outerValue: 0, innerValue: 0, outerIndex: 0, innerIndex: 0}
  8. {outerValue: 0, innerValue: 1, outerIndex: 0, innerIndex: 1}
  9. {outerValue: 1, innerValue: 0, outerIndex: 1, innerIndex: 0}
  10. {outerValue: 1, innerValue: 1, outerIndex: 1, innerIndex: 1}
  11. */
  12. const subscribe = example.subscribe(val => console.log(val));

[RxJS] How To get the results of two HTTP requests made in sequence的更多相关文章

  1. Fedora 24中的日志管理

    Introduction Log files are files that contain messages about the system, including the kernel, servi ...

  2. Burp Suite使用详解一

    本文由阿德马翻译自国外网站,请尊重劳动成果,转载注明出处 Burp Suite是Web应用程序测试的最佳工具之一,其多种功能可以帮我们执行各种任务.请求的拦截和修改,扫描web应用程序漏洞,以暴力破解 ...

  3. Flooded!

    Flooded! Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5955   Accepted: 1800   Specia ...

  4. python——有一种线程池叫做自己写的线程池

    这周的作业是写一个线程池,python的线程一直被称为鸡肋,所以它也没有亲生的线程池,但是竟然被我发现了野生的线程池,简直不能更幸运~~~于是,我开始啃源码,实在是虐心,在啃源码的过程中,我简略的了解 ...

  5. Burp SuiteBurp Suite使用详解

    http://www.2cto.com/Article/201209/153312.html Burp Suite是Web应用程序测试的最佳工具之一,其多种功能可以帮我们执行各种任务.请求的拦截和修改 ...

  6. remove adapter

    Although adapter and other technical sequences can potentially occur in any location within reads, b ...

  7. burp suite 使用教程详解(外文翻译转)

    Burp Suite是Web应用程序测试的最佳工具之一,其多种功能可以帮我们执行各种任务.请求的拦截和修改,扫描web应用程序漏洞,以暴力破解登陆表单,执行会话令牌等多种的随机性检查.本文将做一个Bu ...

  8. python 大数据分析

    #coding:utf-8 ''' @author solq by 2016-01-06 main(目录,正则过滤文件名) 执行完最后打印结果 ''' import json import fnmat ...

  9. sphinx在c#.net平台下使用(一)

    Sphinx是由俄罗斯人Andrew Aksyonoff开发的一个可以结合MySQL,PostgreSQL全文检索引擎.意图为其他应用提供高速.低空间占用.高结果 相关度的全文搜索功能.是做站内全文搜 ...

随机推荐

  1. JS对浏览器Cookie的操作,查询、设置以及删除

    JavaScript是运行在客户端的脚本,因此一般是不能够设置Session的,因为Session是运行在服务器端的. 而cookie是运行在客户端的,所以可以用JS来设置cookie. 假设有这样一 ...

  2. python-打开网页

    最近一直想通过python来实现网页的操作.因为想把自己vimrc的配置直接通过脚本来实现.比较理想的情况下,就是通过一个脚本,把自己需要的一些资源直接从网上下载下来,然后再对下载的资源进行安装等操作 ...

  3. ubuntu adb 安装

    ubuntu 下adb 安装,其实就是下载一个adb,然后给它赋予可执行权限,最后在环境变量里添加一下罢了.具体如下 1.下载adb 这个工具其实是在sdk工具包里面的platform-tools文件 ...

  4. Stacked Autoencoders

    转自:http://www.cnblogs.com/tornadomeet/archive/2013/03/25/2980357.html 如果使用多层神经网络的话,那么将可以得到对输入更复杂的函数表 ...

  5. js 判断是不是空、值是否存在

    判断数组是否存在某个值: Array.indexOf(val) > -1 //存在 (缺陷:一是不够语义化,它的含义是找到参数值的第一个出现位置,所以要去比较是否不等于-1,表达起来不够直观.二 ...

  6. VMware Vsphere 6.0安装部署 Vsphere ESXi安装

    Vsphere ESXi安装 ESXi作为虚拟化环境的Hypervisor层,负责将服务器虚拟成资源池,提供接口供管理组件调用,将下面的iso刻录成光盘或可启动U盘,安装在服务器裸机上: 下载地址请见 ...

  7. 【Android 面试基础知识点整理】

    针对Android面试中常见的一些知识点整理,Max 仅仅是个搬运工.感谢本文中引用文章的各位作者,给大家分享了这么多优秀文章.对于当中的解析,是原作者个人见解,有错误和不准确的地方,也请大家积极指正 ...

  8. POJ 3461 Oulipo KMP算法题解

    本题就是给出非常多对字符串,然后问一个字符串在另外一个字符串出现的次数. 就是所谓的Strstr函数啦. Leetcode有这道差点儿一模一样的题目. 使用KMP算法加速.算法高手必会的算法了. 另外 ...

  9. 码农的救赎:使用Github Pages搭建博客

    人生若仅仅如初见,都恨太晚 据说有博客的人比没博客人的薪水要高非常多,相传写博客也是一个高手的标配,尽管之前一直有在写(在这里).可是孤既不是高手.薪水还比别人少.之前一直在CSDN上面写博客,那是一 ...

  10. UML中的用例图

    用例图构成:參与者(actor).用例(use case).子系统(subsystem) 关联(Association) 泛化(Inheritance) 就是通常理解的继承关系,子用例和父用例类似,但 ...