typescript 等待 observable 完成
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43366694/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Waiting for an observable to finish
提问by
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.
我有一个方法需要等待一个可观察对象完成。我知道 observable 非常适合随着时间的推移返回单个数据,但我需要知道这个 observable 何时完全完成返回其所有数据,以便我可以在它返回的对象上运行验证代码。
The method getCustom subscribes to an observable run on the supplied url which then returns the observable.
getCustom 方法订阅在提供的 url 上运行的 observable,然后返回 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
});
}
采纳答案by VikingCode
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
})
}
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.
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
可观察数据服务的核心是 RxJs 主题。主体实现了 Observer 和 Observable 接口,这意味着我们可以使用它们来发出值和注册下标。
该主题只不过是一个传统的事件总线,但功能更强大,因为它为所有 RxJs 功能运算符提供了它。但从本质上讲,我们只是使用它来订阅,就像常规的 observable 一样
source: angular-university.io
OR you can use Observable.fromPromise(promise) but this will make things a bit more complicated to understand if you are new to ng2
或者你可以使用 Observable.fromPromise(promise) 但是如果你是 ng2 的新手,这会让事情变得更复杂