为什么Angular将Observable用于HttpClient? [英] Why Angular uses Observable for HttpClient?
问题描述
根据 https://angular.io/tutorial/toh-pt6
通常,一个可观察对象可以随时间返回多个值.一个 从HttpClient观察到的总是发出单个值,然后 完成,再也不会发射了.
In general, an observable can return multiple values over time. An observable from HttpClient always emits a single value and then completes, never to emit again.
这确实是正确的,一旦请求完成,Http请求/响应就无法再产生任何值.那么,HTTPClient在发出请求时返回Observable的主要原因是什么?仅仅是因为我们可以在Observable上应用大量运算符(重试,去抖动等)吗?还是我还有其他特定原因吗?
Which is indeed true, Http request/response can't produce any more values once the request completes. So what is the main reason HTTPClient returns an Observable while making a request ? Is it simply because we can apply huge set of operators on Observable (retry, debounce and so on) ? or Is there any other specific reason I am missing ?
推荐答案
您将找到的最佳答案是 Pascal Precth 的一位负责人解释说,在2015年12月问的一个专门问题上:"Observables对HTTP有意义吗?" (但请随时阅读,很多其他答案也确实不错!)
The best answer you'll find is Pascal Precth's one who explained that on a dedicated issue asked in December 2015: "Do Observables make sense for http?" (but feel free to read along, plenty of additional answers are really good too!)
在我的头上:
-重试
-取消
-享受所有Rxjs运算符
-可以将它们合并为流
-在整个应用程序中进行反动思考
-一致性
-可观察对象本质上是冷的,如果您想稍后触发它,则无需像Promises一样将它们包装到工厂函数中
On top of my head:
- Retry
- Cancel
- Enjoy all the Rxjs operators
- Possibility to combine them as streams
- Thinking reactively in your whole app
- Consistency
- Observables are cold by nature, no need to wrap them like Promises into a factory function if you want to trigger it later
这篇关于为什么Angular将Observable用于HttpClient?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!