redux-deferred
Redux middleware for jQuery Deferred Object.
Preface
To create an AJAX request in jQuery is easy.
$
It is not known by everyone that all AJAX method in jQuery return an Deferred Object for you to use. So above code can be rewriten like this:
let deferredObject = $;deferredObject
Using this middleware you can wrap the request into the payload of an action and dispatch it like any normal actions.
let action = type:'LOAD_SOME' payload: deferredObject store;
Since Deferred is often async, the action wrapping it will not be dispatched until it is resolved or rejected.
And when it does, the action will be really dispatched with payload changed to the result value of the deferred.
{ }
Error handling
If the request fail, your can get error detail infomation in action's payload
property.
While action's error
property equals true.
{ }
That's all. The redux-deferred
core code is below 30 lines. And heavily inspired by redux-promise.
Usage
Install
npm install redux-deferred
ES6
;let reducers= YOUR_REDUCER_DEFINITION;let store = ;
Reference
Example
License
MIT