I have used Redux-Saga. Code written with it is easy to reason so far, except JS generator function is messing up my head from time to time. From my understanding, Redux-Obs
Disclaimer: I am one of the authors of redux-observable so it's hard for me to be 100% impartial.
We don't currently provide any reason redux-observable is better than redux-saga because...it's not.
I use Redux-Observable over Redux-Saga because prefer to work with observables over generators. I use it with RXJS, which is a powerful library for working with streams of data. Think of it like lodash for async. In terms of any downsides, gotcha and compromises in choosing one over the other, take a look at this answer from Jay Phelps:
redux-saga as a project has existed longer than redux-observable so that's certainly one major selling point. You'll find more documentation, examples, and likely are have a better community to get support from.
The counter being that the operators and APIs you learn in redux-saga aren't nearly as transferable as learning RxJS, which is used all over the place. redux-observable is super super super simple internally, it's really just giving you a natural way for you to use RxJS. So if you know RxJS (or want to), it's an extremely natural fit.
My advice at the moment for most people is that if you have to ask which one you should use, you probably should choose redux-saga.
Redux-Observable is an amazing library, we use it in production for 1.5 years without any issues so far, it's perfectly testable and can be easily integrated with any framework. We are having extremely overloaded parallel socket channels and the only thing which is saving us from freezes is Redux-Observable
I have 3 points I'd like to mention here.
1. Complexity and learning curve
Redux-saga easily beats redux-observable here. If you need just a simple request to get authorization done and you don't want to use redux-thunk for some reasons, you should consider using redux-saga, it's just easier to understand.
If you don't have prior knowledge of Observable it will be a pain for you and your team will course you :)
2. What can Observable and RxJS offer to me?
When it comes to async logic Observable is your Swiss knife, Observable can literally do almost everything for you. You should never compare them to promises or generators it's far more powerful, it's same like comparing Optimus Prime with Chevrolet.
And what about RxJS? It's like lodash.js but for async logic, once you in you will never switch to something different.
3. Reactive extension
Just check this link
http://reactivex.io/languages.html
The reactive extension is implemented for all modern programming languages, it's just your key to functional programming.
So spend your time wisely learn RxJS and use redux-observable :)
I value the transferability across languages and runtimes that Rx has. Even if your app won't change languages, your career may. Get the best leverage you can on your learning, however you size that up for yourself. It's such a great gateway to .Net LINQ in particular.
I think there are things that you need to take in consideration.
Lets say we want to fetch user from API
// Redux-Saga
import axios from 'axios'
function* watchSaga(){
yield takeEvery('fetch_user', fetchUser) // waiting for action (fetch_user)
}
function* fetchUser(action){
try {
yield put({type:'fetch_user_ing'})
const response = yield call(axios.get,'/api/users/1')
yield put({type:'fetch_user_done',user:response.data})
} catch (error) {
yield put({type:'fetch_user_error',error})
}
}
// Redux-Observable
import axios from 'axios'
const fetchUserEpic = action$ =>
action$
.ofType('fetch_user')
.flatMap(()=>
Observable.from(axios.get('/api/users/1')) // or use Observable.ajax
.map(response=>({type:'fetch_user_done', user:response.data}))
.catch(error => Observable.of({type:'fetch_user_error',error}))
.startWith({type:'fetch_user_ing'})
)
Also, I have written this article in order compare the differences between Redux-saga and Redux-Observable in depth. Check out this link here or presentation.
Since there's a whole bunch of redux-observable talk in here, I'd thought I'd give the saga side of the argument. I don't use redux-observable or RxJS, so I can't give a side by side comparison, but I have used sagas to great effect.
For what its worth, I'm using sagas in production in a web application.
Saga wins hands down. I didn't like how thunk put logic in my action creators. It also made doing a few requests in a row troublesome. I briefly looked at redux-observable for this job, but settled on Sagas.
Understanding what generators are and why they're important is key to understanding sagas. But I will stress that you don't need to know generators inside and out. You only need to know that you're passing off control with the yield statement, and that the saga will pass back control after your async code resolves. After that bit, its not very hard to understand what's going on in a saga.
The core saga methods are (in my experience):
call
- Call any bit of code and get the return value. Supports promises. Great synergy between async processing and sagas.select
- Call a selector. This bit is rather brilliant. Selectors are core to redux, and they are 100% supported!put
- aka dispatch
an action. In fact dispatch as many as you want!There are other functions, but if you can master those three, you will be in a really good spot.
The reason I chose sagas was the ease of use. redux-observable looked like a challenge. I am 100% satisfied with sagas. More happy than I ever expected.
In my experience, Sagas are (way) better than thunks and relatively easy to understand. Rx is not everyone's cup of tea. I would strongly consider sagas instead of redux-observable if you don't come from that ecosystem and/or don't plan on using Rx in the future.