Search code examples
reactjsreduxredux-promise

Redux-Promise does not prevent rejected promises


Redux-Promise says: If it receives a promise, it will dispatch the resolved value of the promise. It will not dispatch anything if the promise rejects.

but when I run the code below and do something intentionally to get the promise rejected in action create later I get that rejected promise on reducer too! shouldn't it be NOT sent to the reducer? PLEASE do NOT tell me just the workaround but also tell me why it is happening when Redux-Promise says so.

action:

  const responce = axios.get(API_URL);  
  console.log(responce);
    return (
        {
            type: FETCH_WEATHER,
            payload: responce,
        }
    );

the responce ^ is: enter image description here

reducer:

   export default (state = initalState, action) => {

        switch (action.type) {
            case FETCH_WEATHER:
            console.log(action.payload);
                return (
                    [action.payload.data, ...state]
                );
            default: return (state);
        }
    }

the action.payload ^ is:

enter image description here


Solution

  • Repeating the phrasing:

    If it receives a promise, it will dispatch the resolved value of the promise. It will not dispatch anything if the promise rejects.

    If it receives an Flux Standard Action whose payload is a promise, it will either

    • dispatch a copy of the action with the resolved value of the promise, and set status to success.
    • dispatch a copy of the action with the rejected value of the promise, and set status to error.

    So, there's two different ways to use this middleware:

    // dispatch a promise directly
    dispatch(somePromise);
    
    // dispatch a promise as an action payload
    dispatch({type : "SOME_ACTION", payload : somePromise})
    

    The "will not dispatch anything if the promise rejects" phrase refers to the first usage - passing a promise directly to dispatch(). You're using it the second way - passing a promise as a payload.