Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Would love to see more examples on usage to replace Thunks for example and other use cases... #21

Open
born2net opened this issue Jan 26, 2016 · 1 comment

Comments

@born2net
Copy link

Would love to see more examples on usage to replace Thunks for example and other use cases...

regards

Sean

@deanrad
Copy link

deanrad commented Sep 26, 2016

An Observable is just a more ordered version of a thunk. The way folks use thunks in Redux is typically to dispatch 1 or more actions, possibly async, and end in success or error. But a thunk is in fact a Javascript executable function, not simply a data structure. Its better to use a more specific type of structure.

An Observable has behavior over time, like a highly crafted async JS function with callbacks, but the semantics for dealing with it are more tightly defined. In particular, exceptions are turned into onError events.

var o = new Observable(observer => 
  o.onNext({type: 'BEGIN'})
  fetch('url')
     .then(r => o.onNext({type: 'SUCCESS', payload: r}))
     .catch(e => o.onError({type: 'FAILED', payload: e}))
)

store.dispatch(o)

// OR, using RxJS magical stream operators which allow flexible stream converstion
store.dispatch(o.timeout(2500))

So that's what dispatching a (handcrafted) Observable would look like.
You can also create Observables from Promises, Arrays, thunks, etc.. So in fact a possibly-terminating series of events is an excellent model for async actions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants