为了账号安全,请及时绑定邮箱和手机立即绑定

Rxjs的pipe怎么像Promise的then一样等待ajax结束才继续?

Rxjs的pipe怎么像Promise的then一样等待ajax结束才继续?

繁花不似锦 2018-08-05 13:47:59
请问在Angular中,如果一个请求依赖于另外一个请求的结果,用Rxjs该怎么处理呢?this.getOne().then(data => {  // 这里返回另外一个Promise   return this.getTwo(data); }).then(data => {   console.log(data);  // 这里打印第二个Promise的值   return this.getThree(data); }).then(data => {   console.log(data); // 这里打印第三个Promise的值})上面是用Promise实现的效果,请问用Rxjs的pipe怎么达到类似的目的呢?
查看完整描述

1 回答

?
慕姐8265434

TA贡献1813条经验 获得超2个赞

mergeMap:

from(this.getOne)
    .pipe(
        mergeMap(oneData => {            console.log(oneData)            return from(this.getTwo)
        }),
        mergeMap(twoData => {            console.log(twoData)            return from(this.getThree)
        })
    )
    .subscribe(threeData => {        console.log(threeData)
        ...
    })


查看完整回答
反对 回复 2018-08-09
  • 1 回答
  • 0 关注
  • 2261 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信