lelandrichardson / redux-pack

Sensible promise handling and middleware for redux
MIT License
1.33k stars 60 forks source link

No way to get the promise result in 'success' #57

Closed ibc closed 7 years ago

ibc commented 7 years ago
// actions.js
export function loadFoo(id) {
  return {
    type: LOAD_FOO,
    promise: Api.getFoo(id),
  };
}
// reducer.js
import { handle } from 'redux-pack';

export function fooReducer(state = initialState, action) {
  const { type, payload } = action;
  switch (type) {
    case LOAD_FOO:
      return handle(state, action, {
        start: prevState => ({
          ...prevState,
          isLoading: true,
          fooError: null
        }),
        finish: prevState => ({ ...prevState, isLoading: false }),
        failure: prevState => ({ ...prevState, fooError: payload }),
        success: prevState => ({ ...prevState, foo: payload }),
      });
    default:
      return state;
  }
}

So, how to use/get the result of Api.getFoo(id) within the reducer?

tony-kerz commented 7 years ago

i believe payload will contain the result of the promise for non-failure cases, is that what u are asking...?

ibc commented 7 years ago

Ok, so in success I should just call payload.promise().then((result) => so I get the result. Right, sorry.

BTW should the promise be set into action? Or into action.payload? The example uses action.promise but it fails when calling handle later (bad usage reported by the lib itself).

tony-kerz commented 7 years ago

payload should be the result of the promise, not the promise itself, so easier than what u were thinking... 👍

ibc commented 7 years ago

Clear. Thanks.