redux-pirate-promise
Redux promise middleware enables robust handling of async code in Redux. The middleware enables optimistic updates and dispatches pending, fulfilled and rejected actions. It can be combined with redux-thunk to chain async actions.
;; { return ; { datatype = `Request`; ; } { let error = data; let err = errordata && errordataerror || error; dataerror = err; datatype = `Error`; ; } } promiseMiddleware;createStore;
There you have two options to use promise in your actions. First is common way define a promise
property.
const foo = type: 'FOO' promise: ;;
Or monkey patching the promise object.
const foo = { return Object;};
The second wariant is very useful when you work a lot with certain API methods, often returning promises at the moment.
Installation
npm install redux-promise-middleware -s
Features
❁ Handle promise process:
The library provide possibility to handle request
, response
, error
globally via overriding the default params in the redux-pirate-promise
. By the default it doenst modify the flow. You can work with promises like with pure function without any overhead.
let {} = params;
By we found often it's good to trigger store on the request. For examples to show the progress to end user.
;; let params = { datatype = 'LOADING'; ; }; promiseMiddleware;createStore;
ϟ Global error handling: The often case, the promise return the error. You needs to handle it some how.
//store.js;; let params = { datatype = `Error`; ; }; promiseMiddleware;createStore;
//reducers.js { if actiontype return ...state actionerror; if actiontype === typesclearErrors return ; return state;}
It allow you format error message one place and show errors popup simpler way.
❤ Action chaining: As for me the redux-thunk is overcomplicated to use. Let's tho a bit we already have promises in many places: API, Async / Await methods, external libraries why do we drop native promise changing wrapping promises to object?
Like redux-promise-middleware do
// implicit promiseconst foo = type: 'FOO' payload: ; // explicit promiseconst foo = type: 'FOO' payload: promise: ;
When we extends promise object with redux
type constant. We can have native support of chaning, and no need to bring complex solutions like redux-thunk
.
const foo = { return Object;}; const bar = { return Object;};
So later in the application we can do like that:
;
or using async await
await ;await ; let state = store;
Copyright (c) 2017 pirateminds.com. Licensed with The MIT License (MIT). strictly enforced.