我应该总是在可以使用承诺的地方使用 Observables 吗? [英] Should I always use Observables where a promise can be used?

查看:53
本文介绍了我应该总是在可以使用承诺的地方使用 Observables 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我昨天发布了一个关于堆栈溢出的问题,我问用户群是否有办法在不显式使用 asyncawait 的情况下等待异步操作的结果关键词.原因是我需要等待操作完成,以便我可以在触发任何其他 HTTP 请求之前使用返回的值.事实证明,它涉及使用 observable 订阅操作的结果,然后使用 mergeMap 继续处理其他请求.

I posted a question on stack overflow yesterday where I asked the userbase if there was a way to await the result of an async operation without explicitly using the async and await keywords. The reason being that I needed to wait for the operation to complete so that I could use the value returned before any other HTTP requests could be fired. It turns out there was, it involved using observables to subscribe to the result of the operation, and to then use a mergeMap to continue on with the other requests.

前几天我还读了一篇文章,其中概述了许多 JS/RxJs/Angular 开发人员通过将它们用于每个可能的异步操作来滥用"可观察对象,即使只有一个值被返回.文章概述了在这种特殊情况下 promise 会更合适,因为 observable 被认为是矫枉过正.

I also read an article the other day that outlined that a lot of JS/RxJs/Angular developers were 'abusing' observables by using them for every possible async operation, even where there was only a single value being returned. The article outlined that in this particular case promises would be more suitable, as observables were seen to be overkill.

这让我想知道是否有任何特定的场景或原因我应该使用 promises 而不是 observables.在我自己看来,observables 提供了对异步操作的更大控制,并且还通过使用 RxJS 提供的 let-able 运算符提供了很多功能.这一点,再加上仅返回单个值的异步操作仍然可以由 Observables 处理(因为它们只是一个只有一个值的流)这一事实让我认为确实没有特定的scernario 我曾经想在可观察对象上使用承诺.这样的场景是否存在?

This has got me wondering if there is any particular scenarios or reasons where I should be using promises over observables. In my own opinion observables offer much greater control over an async operation and also provide far much functionality through the use of the let-able operators provided by RxJS. This, combined with the fact that async operations that only return a single value can still be handled just as well by Observables (since they're simply a stream that only has a single value) makes me think that there really isn't a particular scernario where I would ever want to use a promise over an observable. Does such a scenario exist?

我希望这个问题不要太主观,因为优点和缺点或承诺和可观察量都是两种编程结构的客观属性.所以希望我不应该看到这个问题被关闭.

I hope this question isn't too subjective, as the advantages and disadvantages or promises and observables are both objective properties of the two programming constructs. So hopefully I shouldn't see this question closed.

推荐答案

作为一名 Angular 开发人员,我不喜欢看到 Promise 被使用,因为我们正在以响应式编程风格构建基于 observable 的应用程序.

As an Angular developer I don't like to see promises being used as we are building applications built on observables in a reactive programming style.

没有滥用 observables,因为可以使用 promise 来代替.

There is no abusing observables because a promise could be used instead.

Angular http 服务可以使用承诺作为 http 请求是一个火灾并获得单个响应场景,不是流,但在这种情况下,observable 仍然完全有意义.

The Angular http service could use promises as a http a request is a fire and get a single response scenario, not a stream but an observable still makes complete sense in this case.

我会争辩说,既然我们已经有了用于异步操作的可观察对象,为什么还要将 promise 引入反应式编程应用程序.

I would argue that why bring promises into a reactive programming application when we already have observables for async operations.

我 100% 的观点是,Angular 开发人员绝对不会在可以使用 promise 的用例中滥用 observable.使用单个异步样式比混合和匹配样式更一致,并且还可以让您在以后的 http 请求变成流时进行重构.

I am 100% of the opinion that Angular devs are definitely not abusing observables in a uses case where a promise could be used. Using a single async style is more consistent that a mix and match style and it also lets you refactor at a later date when your http request becomes a stream.

这篇关于我应该总是在可以使用承诺的地方使用 Observables 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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