Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between redux-thunk and redux-promise?

As far as I know and correct me if I am wrong, redux-thunk is a middleware which helps us dispatch async function and debug values in the action itself while when I used redux-promise I couldn't create async functions without implementing my own mechanism as Action throws an exception of dispatching only plain objects.

What is the major differences between these two packages? Are there any benefits of using both the packages in a single page react app or sticking to redux-thunk would be enough?

like image 814
shet_tayyy Avatar asked Apr 12 '16 15:04

shet_tayyy


People also ask

How is redux thunk different from redux?

In thunk, action creator does not return an object, it returns a function, In the saga, it allows you to send action normally. but it has a watcher. whenever a particular action gets dispatched, the watcher catches it.

What is redux promise?

redux-promise "teaches" dispatch how to accept promises, by intercepting the promise and dispatching actions when the promise resolves or rejects. Normally, dispatch returns whatever action object was passed in. Because middleware wrap around dispatch , they can also change what value is being returned.

What is the difference between Redux thunk and Redux-saga which one should I use How do you decide between thunks sagas is it fine to use both?

Saga works like a separate thread or a background process that is solely responsible for making your side effects or API calls unlike redux-thunk, which uses callbacks which may lead to situations like 'callback hell' in some cases. However, with the async/await system, this problem can be minimized in redux-thunk.

Which one is better Redux-saga or Redux thunk?

But on the other hand, for bigger projects, Redux-Thunk may sometimes get you in trouble, as it can be hard to scale if your side effect or asynchronous logic increases, whereas in the case of Redux-Saga, it comes power-packed with some amazing things such as concurrent side effects, canceling side effects, debouncing ...


1 Answers

redux-thunk allows your action creators to return a function :

function myAction(payload){     return function(dispatch){         // use dispatch as you please     } } 

redux-promise allows them to return a promise :

function myAction(payload){     return new Promise(function(resolve, reject){         resolve(someData); // redux-promise will dispatch someData     }); } 

Both libraries are useful if you need to dispatch action async or conditionally. redux-thunk also allows you to dispatch several times within one action creator. Whether you choose one, the other or both entirely depends on your needs/style.

like image 118
VonD Avatar answered Sep 20 '22 11:09

VonD