简体   繁体   English

等待Observable的RxJS Observable

[英]RxJS Observable That waits on Observable

So I have an observable which makes an HTTP post to get the access token from a server. 因此,我有一个可观察的对象,该对象进行了HTTP发布,以从服务器获取访问令牌。 I have another which performs a get to the same server, but requires the access token from the first to exist. 我有另一个执行到同一服务器的访问,但是需要从第一个访问令牌存在。 So I would like to be able to subscribe to both observables at the same time in two different places, but the GET observable must of course wait on the POST observable. 因此,我希望能够在两个不同的地方同时订阅两个可观察对象,但是GET可观察对象当然必须等待POST可观察对象。 How can I make an observable wait on another Observables subscribe completion? 如何在另一个Observables订阅完成上进行可观察的等待?

Not sure I understand right but here follows an option. 不确定我是否理解正确,但是这里有一个选项。 Assuming postHttp$ is your access token fetch observable, and getFromServer$ the one performing the get to the server, and assuming those are sequences of only one value (ie promise-like): 假设postHttp$是您的访问令牌可观察到的, getFromServer$是执行获取到服务器的访问令牌,并假设这些序列只有一个值的序列(即类似promise的序列):

  • postHttp$.flatMap(function (authToken){return $.ajax(...)}) will wait for postHttp to have a value to produce a promise which will be flattened down to its resolved value. postHttp$.flatMap(function (authToken){return $.ajax(...)})将等待postHttp具有一个值来产生一个promise,该promise将被扁平化为其解析值。 ie the GET observable (...) wait on the POST observable. the GET observable (...) wait on the POST observable. To retrieve the value, you can subscribe to the observable, or continue chaining other operators to it. 要检索该值,您可以订阅可观察值,或继续将其他运算符链接到该值。
    • flatMap accepts promises as the return value of its selector function parameter, so no need here to convert to Rx.Observable . flatMap接受promise作为其选择器函数参数的返回值,因此此处无需转换为Rx.Observable

Is that what you wanted? 那是你想要的吗?

I figured out a solution so I thought I'd go ahead and post my own answer. 我想出了一个解决方案,所以我想继续发表自己的答案。 If anyone knows a more efficient way of doing things definitely post an answer and I'll accept it! 如果有人知道更有效的做事方式,一定要发布答案,我会接受的!

this.getObservable = Rx.Observable.create(function(observer){
  this.postObservable.subscribe(null, null, function onComplete(){
    var ajaxObservable = Rx.Observable.fromPromise($.ajax({
      url: this.apiPath,
      method: 'GET',
      beforeSend: function (xhr) {
        // this.authToken is created by the postObservable, so we have to subscribe to the oncomplete of that in order to use it.  
        xhr.setRequestHeader('Authorization', this.authToken); 
      }.bind(this)
    }).promise());
    ajaxObservable.subscribe(
      function onNext(data){
        observer.next(data);
      }, 
      function onError(error){
        observer.error(error);
      }, 
      function onComplete(){
        observer.complete();
      }
    );
  }.bind(this));
}.bind(this));

In this code, getObservable subscribes to postObservable, and only once that completes does it make it's own Ajax call. 在这段代码中,getObservable订阅了postObservable,只有完成后,它才会进行自己的Ajax调用。 This is useful because it lets my pages immediately subscribe to whatever GET observable they want, and not have to worry about the callback hell of subscribing first to the postObservable, then subsribing to the next observable when that's ready. 这很有用,因为它可以让我的页面立即预订所需的任何GET可观察对象,而不必担心先订阅postObservable,然后在准备好后订阅下一个可观察对象的回调地狱。 To the individual page, the post request is completely hidden, they just subscribe to the get request. 对于单个页面,发布请求被完全隐藏,他们只是订阅了get请求。

Note that this code would probably be a lot cleaner with arrow functions but I'm trying to stay compliant with some older browsers. 请注意,此代码使用箭头功能可能会更整洁,但我试图保持与某些旧版浏览器的兼容性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM