鍍金池/ 問答/HTML5  網(wǎng)絡(luò)安全  HTML/ Rxjs的pipe怎么像Promise的then一樣等待ajax結(jié)束才繼續(xù)?

Rxjs的pipe怎么像Promise的then一樣等待ajax結(jié)束才繼續(xù)?

請問在Angular中,如果一個請求依賴于另外一個請求的結(jié)果,用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實現(xiàn)的效果,請問用Rxjs的pipe怎么達到類似的目的呢?

回答
編輯回答
別逞強

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年9月22日 13:47