You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is the case: in my components I want to do some custom actions depends on whether the api call succeeds or fails, like redirecting or showing some messages. I want something like this
But the problem is redux-promise returns the result of the promise.then() instead of the original promise itself, and according to the answers here http://stackoverflow.com/questions/34222818/native-promise-chaining-with-catch, then the promise i receive from dispatch will always go to onResolve and never onReject (in case of error it will resolve an undefined result). So should redux-promise return the original promise so that we can register then() and catch() to the returned result of dispatch()?
The text was updated successfully, but these errors were encountered:
If I dispatch an async action, the dispatch() method will return a promise which, when resolved, will contain the original action. If you use redux-actions you can then inspect the action if it has error = true set and you know that your original promise was rejected.
Here's some sample code that will demonstrate that:
import{createStore,applyMiddleware}from'redux';importpromiseMiddlewarefrom'redux-promise';importRSVPfrom'rsvp';import{createAction,handleAction}from'redux-actions';constreducer=handleAction('TEST_ASYNC',(state={},action)=>(Object.assign({},state,{payload: action.payload})));conststore=createStore(reducer,applyMiddleware(promiseMiddleware));constpromise=newRSVP.Promise((resolve,reject)=>{constrand=Math.ceil((Math.random()*10)+1);console.log(rand);if(rand%2===0){setTimeout(()=>{resolve("resolved");},1000);}else{reject("rejected");}});constaction=createAction('TEST_ASYNC');constdispatchResult=store.dispatch(action(promise));console.dir(dispatchResult);dispatchResult.then((result)=>{if(result.error){console.log(`Promise was rejected with payload: ${result.payload}`)}else{console.log(`Promise was resolved with payload: ${result.payload}`)}});
This is the case: in my components I want to do some custom actions depends on whether the api call succeeds or fails, like redirecting or showing some messages. I want something like this
But the problem is redux-promise returns the result of the
promise.then()
instead of the original promise itself, and according to the answers here http://stackoverflow.com/questions/34222818/native-promise-chaining-with-catch, then the promise i receive fromdispatch
will always go to onResolve and never onReject (in case of error it will resolve anundefined
result). So should redux-promise return the original promise so that we can registerthen()
andcatch()
to the returned result ofdispatch()
?The text was updated successfully, but these errors were encountered: