等待一个可观察的事物完成 [英] Waiting for an observable to finish

查看:43
本文介绍了等待一个可观察的事物完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个方法需要等待可观察对象完成.我知道Observable对于随着时间的推移返回单个数据非常有用,但是我需要知道何时该Observable完全完成了所有数据的返回,以便我可以在返回的对象上运行验证代码.

I have a method that needs to wait for an observable to finish. I know observable are great for returning single pieces of data over time but I need to know when this observable has completely finished returning all of its data so I can run validation code on the object it has returned.

getCustom方法订阅在提供的url上的可观察的运行,然后返回可观察的.

The method getCustom subscribes to an observable run on the supplied url which then returns the observable.

我不太确定这是否是解决这种情况的最佳方法,因此,如果有人可以给我任何建议或指导来解决这个问题,我将不胜感激.

I am not too sure if this is the best way to approach this situation so I would appreciate if anyone could give me any advice or a direction to handle this.

  private validateQuoteRetrievalAnswers(reference: string) {

         // Get the risk from the server
        this.riskManager.getRiskFromServer(reference);

        if (this.riskManager.risk) {
            // Validate risk that was returned
        }
    }

getRiskFromServer(quoteReference: string) {

    this.riskService.getCustom("Url").subscribe => {
        // need to know when the observable has returned the risk
    });

}

推荐答案

我将如何应对这一挑战:

how i would tackle this challenge:

查询您的后端,当我们有需要时将其推送到主题

Query you back-end and when we've got what we need push it to a Subject

riskSubject = new Subject<Risk>();

getRiskFromServer(quoteReference: string) {
  this.riskService.getCustom("Url")
  .subscribe( 
    data => { this.riskSubject.next(data); },
    error => { console.log(error) }
 });
}

然后订阅主题并等待,直到获得所需的内容并开始验证

and then subscribe to subject and wait until you get what you need and start validating

private validateQuoteRetrievalAnswers(reference: string) {

         // Get the risk from the server
        this.riskManager.getRiskFromServer(reference);
        // subscribe to subject
        this.riskManager.riskSubject.subscribe(
         data => {
           //do your validation
        })
}

可观察数据服务的核心是RxJs主题.主题同时实现了Observer和Observable接口,这意味着我们可以使用它们来发出值和注册下标.

The heart of an observable data service is the RxJs Subject. Subjects implement both the Observer and the Observable interfaces, meaning that we can use them to both emit values and register subscriptors.

主题不过是传统的事件总线,而功能却强大得多,因为它为所有RxJs功能运算符提供了它.但从本质上讲,我们只是像常规的可观察对象一样使用它来进行订阅

The subject is nothing more than a traditional event bus, but much more powerful as it provides all the RxJs functional operators with it. But at its heart, we simply use it to subscribe just like a regular observable

来源: angular-university.io

或者您可以使用Observable.fromPromise(promise),但是如果您是ng2的新手,那么这会使事情变得更加复杂

OR you can use Observable.fromPromise(promise) but this will make things a bit more complicated to understand if you are new to ng2

这篇关于等待一个可观察的事物完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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