简体   繁体   English

RxJs异步操作和javascript promise或async等待之间的区别

[英]Difference between RxJs async operations and javascript promise or async await

This might be the silly one but help me out with the question. 这可能是愚蠢的,但可以帮助我解决这个问题。 I started learning RxJs but I could see RxJs is used for reactive programming where we can subscribe to events and handling multiple events and process in chaining with observables and I got the doubt like what will be the difference between using async/await or promises in javascript because they are also doing the async flow in javascript. 我开始学习RxJ,但是我可以看到RxJ用于反应式编程,我们可以在其中订阅事件并处理多个事件以及与Observable进行链接的过程,我对此感到怀疑,例如在javascript中使用async / await或promises有什么区别因为他们也在用javascript执行异步流程。 Any advantages over async/await. 与异步/等待相比的任何优势。

I suggest you search the web a bit as this will give you a much better answer and views on the difference between asynchronous promise and observable objects 我建议您在网络上进行一些搜索,因为这将为您提供更好的答案,并提供有关异步承诺和可观察对象之间区别的观点

The main difference is that you use promise when you want the code to be done asynchronously and you use observables when you want to be notify when a variable value is being altered (you observe the variable by subscribing to its events) 主要区别在于,当您希望异步完成代码时,您可以使用promise;而当变量值被更改时,您需要通知时,您可以使用observables(您可以通过订阅变量的事件来观察变量)。

The main difference: Promise is one-time while Observable is assumed to emit for multiple times. 主要区别在于: Promise是一次,而Observable被假定多次发射。

Events, HTTP streams, interval timers - that all does not fit Promise well. 事件,HTTP流,间隔计时器-都不适合Promise So Observable is more like generators than Promise . 因此, Observable更像是生成器,而不是Promise

Say we need throttling click with requesting server having some short timeout in case server does not respond in 5 seconds. 假设我们需要限制请求服务器的点击速度,以防服务器在5秒内没有响应,因此超时时间较短。 We can either use event handler wrapped into throttle and then running Promise-based fetch with attaching promisified timeout with Promise.race() . 我们可以使用包裹在throttle事件处理程序,然后通过将Promise.race()附加有约定的超时来运行基于Promise的fetch

Or we can use only rxjs to describe complete flow as set of operators. 或者我们只能使用rxjs将完整的流程描述为一组运算符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM