简体   繁体   English

RxJS等到承诺解决了

[英]RxJS wait until promise resolved

I'm still figuring out reactive programming so I'm pretty sure this is very basic, but the number of stream transformations is pretty overwhelming to a beginner. 我仍在计算反应式编程,所以我很确定这是非常基本的,但是对于初学者来说,流转换的数量非常大。

I'm creating an Observable from a DOM event. 我正在从DOM事件创建一个Observable。 This event should in turn trigger a REST call and all other DOM events will be ignored until this event has been resolved. 此事件应依次触发REST调用,并且在解决此事件之前,将忽略所有其他DOM事件。

const stream = Observable.fromEvent(document, 'some-event')
stream
  .flatMap(() => httpRestService())
  .subscribe(() => {
  })

How do I ignore the events from the stream until the last HTTP promise has resolved? 在最后一个HTTP承诺解决之前,如何忽略流中的事件?

DOM event
A - - - - B - - - - C
HTTP event
D ...........done - C

You could try flatMapFirst which seems to do what you want. 您可以尝试flatMapFirst ,它似乎可以做你想要的。 The following code could work ( jsfiddle here - click anywhere) : 以下代码可以工作( jsfiddle在这里 - 单击任意位置):

const stream = Observable.fromEvent(document, 'some-event')
stream
  .flatMapFirst(() => httpRestService())
  .subscribe(() => {
  })

Quoting the documentation : 引用文档:

The flatMapFirst operator is similar to the flatMap and concatMap methods described above, however, rather than emitting all of the items emitted by all of the Observables that the operator generates by transforming items from the source Observable, flatMapFirst instead propagates the first Observable exclusively until it completes before it begins subscribes to the next Observable. flatMapFirst运算符类似于上面描述的flatMap和concatMap方法,但是,不是通过从源Observable转换项来发出运算符生成的所有Observable发出的所有项,而是flatMapFirst将第一个Observable传播到它直到它在开始订阅下一个Observable之前完成。 Observables that come before the current Observable completes will be dropped and will not propagate. 在当前Observable完成之前的Observable将被删除并且不会传播。

UPDATE UPDATE

Looking at the source code ( https://github.com/Reactive-Extensions/RxJS/blob/master/src/core/linq/observable/switchfirst.js ) it seems that while the the current observable has not completed, all the incoming observables in the meantime will be discarded, ie not subscribed to. 看一下源代码( https://github.com/Reactive-Extensions/RxJS/blob/master/src/core/linq/observable/switchfirst.js )似乎虽然目前的观察结果尚未完成,但所有的在此期间传入的可观察量将被丢弃,即未被订阅。

So if subscribing to these observables triggers the http call (would be interesting to see the code for httpRestService ), then there is no unnecessary http call. 因此,如果订阅这些observable会触发http调用(看到httpRestService的代码会很有趣),那么就没有不必要的http调用。 If those calls are triggered immediately by calling the function and the result is passed through an observable, then there is a possibility that those calls are indeed triggered unnecessarily. 如果通过调用函数立即触发这些调用并且结果通过observable传递,那么这些调用确实可能被不必要地触发。 In which case, that issue is easily solvable with using the defer operator to do the http call only at subscription time. 在这种情况下,使用defer运算符仅在订阅时执行http调用就可以轻松解决该问题。 In short, you need lazy execution of the rest request if you don't already have it. 简而言之,如果您还没有它,则需要延迟执行其余请求。

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

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