RxJS 等到承诺解决 [英] RxJS wait until promise resolved

查看:19
本文介绍了RxJS 等到承诺解决的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仍在研究响应式编程,所以我很确定这是非常基础的,但是流转换的数量对于初学者来说是相当难以承受的.

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.

我正在从 DOM 事件创建一个 Observable.此事件应依次触发 REST 调用,所有其他 DOM 事件将被忽略,直到此事件得到解决.

I'm creating an Observable from a DOM event. This event should in turn trigger a REST call and all other DOM events will be ignored until this event has been resolved.

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

如何忽略流中的事件,直到最后一个 HTTP 承诺得到解决?

How do I ignore the events from the stream until the last HTTP promise has resolved?

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

推荐答案

你可以试试 flatMapFirst 似乎可以满足您的需求.以下代码可以工作(jsfiddle here - 单击任意位置):

You could try flatMapFirst which seems to do what you want. The following code could work (jsfiddle here - click anywhere) :

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

引用文档:

flatMapFirst 操作符类似于上面描述的 flatMap 和 concatMap 方法,但是,不是发射操作符通过转换源 Observable 中的项目生成的所有 Observables 发射的所有项目,flatMapFirst 而是传播第一个 Observable直到它在开始订阅下一个 Observable 之前完成.在当前 Observable 完成之前到达的 Observable 将被删除并且不会传播.

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. Observables that come before the current Observable completes will be dropped and will not propagate.

更新

看源码(https://github.com/Reactive-Extensions/RxJS/blob/master/src/core/linq/observable/switchfirst.js) 看来虽然当前的observable还没有完成,所有传入的observable在此期间将被丢弃,即不订阅.

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, i.e. not subscribed to.

因此,如果订阅这些 observable 会触发 http 调用(查看 httpRestService 的代码会很有趣),那么就没有不必要的 http 调用.如果这些调用是通过调用函数立即触发的,并且结果通过 observable 传递,那么这些调用可能确实被不必要地触发了.在这种情况下,通过使用 defer 运算符仅在订阅时执行 http 调用,可以轻松解决该问题.简而言之,如果您还没有剩余请求,您需要延迟执行它.

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. 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. In which case, that issue is easily solvable with using the defer operator to do the http call only at subscription time. In short, you need lazy execution of the rest request if you don't already have it.

这篇关于RxJS 等到承诺解决的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆