Skip to content

Latest commit

 

History

History
70 lines (53 loc) · 2.1 KB

README.md

File metadata and controls

70 lines (53 loc) · 2.1 KB

Redux thunk + generators

You can use generators as action creators or thunks. Fully compartible with redux-thunk!

NPM Version NPM Downloads GitHub issues Licence

Installation

npm install --save redux-thunk-generators

Just replace redux-thunk import with redux-thunk-generators

Usage

You can use generators (sync or async) as thunks:

export const signIn = payload => async function* (dispatch, getState, extraArgument) { /* ... */ }

Or use generators as action creators:

export const signIn = async function* (payload) { /* ... */ }

Yield action objects to dispatch them! Forget about wrapping each time with dispatch:

// Action creator
export const signIn = async function* (payload) {
  const { username, password } = payload;
  let state = yield; // won't be dispatched, just returns current state
  yield signInStart();
  try {
    const response = await axios.post(API_SIGN_IN, { username, password });
    yield signInEnd();
    yield signInSuccess(response.data);
    return username;
  } catch (error) {
    yield signInEnd();
    yield signInError(error);
  }
};

yield always returns a (new) state.

If you want to do something when your action is done, return some data from generator and get it with .then:

signIn().then(username => {
  console.log(username)
});

Yep, nice) Tell your friend.

Author

@doasync