获得承诺价值后返回可观察性

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了获得承诺价值后返回可观察性相关的知识,希望对你有一定的参考价值。

我有以下代码,但它不起作用,我希望一旦承诺得到解决就返回一个observable。有任何想法吗?任何建议都是受欢迎的

  getParalelos() {

    let _observable;
    this.getToken().subscribe(token => {
      _observable = this.http.get(`${this.url}/paralelo?token=${this.token}`, httpOptions)
    })
    return _observable;
  }
答案

您可以使用flatMap。

http://reactivex.io/documentation/operators/flatmap.html

import { flatMap } from "rxjs/operators";

getParalelos() {
    return this.getToken().pipe(
        flatMap(token => {
            return this.http.get(`${this.url}/paralelo?token=${this.token}`, httpOptions)
        })
    );
}

以上是关于获得承诺价值后返回可观察性的主要内容,如果未能解决你的问题,请参考以下文章