redux-thunk-generators

0.3.1 • Public • Published

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

Package Sidebar

Install

npm i redux-thunk-generators

Weekly Downloads

0

Version

0.3.1

License

MIT

Last publish

Collaborators

  • doasync