问题描述
我如何知道Observable制作人何时处于异步或同步状态?
同步示例:
Observable.of([1,2,3])
另一个异步示例(ngrx商店,请参阅)
this.store.take(1);
现在有一个明显的异步示例:
this.http.get(restUrl)
我完全理解这是如何工作的,一些Observable可以同步和其他异步。我不明白的是我怎么能预先区分出来?例如,生产者有一个明显的界面告诉我它会异步吗?
tl; dr
此处)。
@Sasxa已经(正确)回答我们可以使用同步调用从ngrx商店获取最新值:
function getState(store:Store< State>):State {
let state:State;
store.take(1).subscribe(s => state = s);
返回状态;
}
然而,知道可观察量通常是异步的,我看到了这一点并立即想到了RACE CONDITION !在subscribe调用函数之前,该方法可以返回一个未定义的值。
我通过Store和Observable的内部调试,这个例子确实是同步的,但是如何我应该知道吗?对于那些了解ngrx的人来说,商店
的选择
方法(用于获取最新值)是异步的,没有怀疑,因为这是给我们反应gui的原因,这就是我做出假设的原因。
这意味着我不能重构上面的代码如下:
function getLatest(observable:Observable):any {
let obj:any;
observable.take(1).subscribe(latest => obj = latest);
return obj;
}
我可以用任何Observable调用它,它可以用于同步生成器 - 它对于异步生产者来说甚至可能会工作一些时间,但如果传入async observable,这毫无疑问是竞争条件。
很难弄清楚你得到的可观察是同步还是异步(想象一下你从一个第三方库中获得了可观察的回归)。这就是为什么你以一种控制执行顺序和可预测的方式编写它的原因。
这也是为什么有像concat,combineLatest,forkjoin,switchMap,race,merge这样的运营商可以帮助你获得不同场景的订单和性能的原因
How do I know when an Observable producer is async or sync?
An sync example:
Observable.of([1, 2, 3])
another async example (ngrx Store, see here)
this.store.take(1);
And now an obvious async example:
this.http.get(restUrl)
I fully understand how this works, and that some Observables can be sync and others Async. What I don't understand is how i can tell the difference in advance? Is there for example an obvious interface on the producer that tells me that it will be async?
tl;dr
The main reason this question has come up is because of the answer from the link above (here).@Sasxa has (correctly) answered that we can use a synchronous call to get the latest value from the ngrx store:
function getState(store: Store<State>): State {
let state: State;
store.take(1).subscribe(s => state = s);
return state;
}
However knowing that observables are usually asynchronous, I saw this and immediately thought RACE CONDITION! the method could return an undefined value before subscribe has called back the function.
I've debugged through the internals of Store and Observable and this example is indeed synchronous, but how should I have known that? For those that know ngrx, the select
method of store
(used to get the latest values) is asynchronous, without a doubt, as this is what gives us the reactive gui, which is why I came to the assumption I did.
It means that I cannot refactor the code above as follows:
function getLatest(observable: Observable): any {
let obj: any;
observable.take(1).subscribe(latest => obj = latest);
return obj;
}
I could call this with any Observable and it would work for synchronous producers - it may even work SOME OF THE TIME for async producers, but this is without a doubt a race condition if an async observable is passed in.
It is hard to figure out whether an observable you get is sync or async (imagine you get observables return from a thrid party library). That's why you write it in a fashion that the execution order is controlled and predictable.
That's also why there are operators like concat, combineLatest, forkjoin, switchMap, race, merge to help you get the order and performance right for different scenario
这篇关于可观察的异步与同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!