您如何在现有项目中引入响应式编程?
How do you introduce reactive programming in an existing projet?
可能 "subjective" 问题:将反应式编程(rxjs、bacon 等)引入现有 "old-school- MVC-jquery" 客户端应用程序的最佳点是什么?
例如,如果您在应用程序的 "API calls" 层中引入 Promise,它最闪耀(您可以一次只执行一个功能,返回一个 Promise 而不是接受回调 - 他们,一旦团队中的每个人都得到一些好处,它就会传播开来。)
从我能读到的大部分教程中,我真的看不出 rxjs 最闪耀的地方。在小部件中(返回事件流而不是 'listener-based' API ?)
欢迎任何经验。
It is hard to say where it will shine the most…
For me, the key feature, is that it allows to describe code in more declarative way instead of writing complicated state machines(as it often happens when working with async logic).
In general, it can be quite useful for anything async for example in UI, or for API calls layer implementation as you mentioned about Promises, but better(promise is just a limited version of observable, except the fact that observable is lazy).
In case of implementing API calls layer, in comparison to promises it will have at least following benefits:
- subscription to observable is cancellable(disposable) - for example, you can switch between subscriptions for api results without worrying about race conditions with previous api requests... it is as simple as
results = queries.switchMap(q=>doApiCall(q))
- it can return multiple values using the same interface - you can easily replace ajax call with subscription to web-socket, and you will not need to change code that is using this.
- better error handing - it is quite easy with rx to do things like retrying operation n-times before throwing an error, or handling timeout.
I suggest you to watch Netflix JavaScript Talks - Async JavaScript with Reactive Extensions by Jafar Husain, there is great examples about where RxJS can be helpful. And likely it would be mostly an answer to your question.
可能 "subjective" 问题:将反应式编程(rxjs、bacon 等)引入现有 "old-school- MVC-jquery" 客户端应用程序的最佳点是什么?
例如,如果您在应用程序的 "API calls" 层中引入 Promise,它最闪耀(您可以一次只执行一个功能,返回一个 Promise 而不是接受回调 - 他们,一旦团队中的每个人都得到一些好处,它就会传播开来。)
从我能读到的大部分教程中,我真的看不出 rxjs 最闪耀的地方。在小部件中(返回事件流而不是 'listener-based' API ?)
欢迎任何经验。
It is hard to say where it will shine the most…
For me, the key feature, is that it allows to describe code in more declarative way instead of writing complicated state machines(as it often happens when working with async logic).
In general, it can be quite useful for anything async for example in UI, or for API calls layer implementation as you mentioned about Promises, but better(promise is just a limited version of observable, except the fact that observable is lazy).
In case of implementing API calls layer, in comparison to promises it will have at least following benefits:
- subscription to observable is cancellable(disposable) - for example, you can switch between subscriptions for api results without worrying about race conditions with previous api requests... it is as simple as
results = queries.switchMap(q=>doApiCall(q))
- it can return multiple values using the same interface - you can easily replace ajax call with subscription to web-socket, and you will not need to change code that is using this.
- better error handing - it is quite easy with rx to do things like retrying operation n-times before throwing an error, or handling timeout.
I suggest you to watch Netflix JavaScript Talks - Async JavaScript with Reactive Extensions by Jafar Husain, there is great examples about where RxJS can be helpful. And likely it would be mostly an answer to your question.