Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

can redux saga dispatch return result immediately instead of returning action #1907

Closed
alanhg opened this issue Aug 11, 2019 · 7 comments
Closed

Comments

@alanhg
Copy link

alanhg commented Aug 11, 2019

maybe like this

 async componentDidMount() {
        const userInfo = await this.props.fetchUserAction();
    }
function* fetchUserEffects() {
    console.log('fetchUserEffects');
    const user = (yield call(getUserInfo));
    return user;
    // yield put({type: 'USER_FETCH_SUCCEEDED', user: user.data});
}

function* mySaga() {
    yield takeEvery('USER_FETCH', fetchUserEffects);
    yield takeEvery('TEST_SAGA', testSagaEffects);
}

export default mySaga;
export const fetchUserAction = () => ({
    type: 'USER_FETCH'
});

benefit

I can use the effect's result directly, don't need to put action and save data to redux store.

dva.js

According to my understanding, dva.js is based on the framework of redux-saga and other technologies. When we dispatch the action in this framework and execute saga-effects, we can get the result directly. I think this is a very good feature.

about the question

https://stackoverflow.com/questions/52214499/can-redux-saga-dispatch-return-api-result-immediately-instead-of-returning-actio

@aybb
Copy link

aybb commented Aug 14, 2019

You don't call saga directly, you actually call store.dispatch(action).

@alanhg
Copy link
Author

alanhg commented Aug 15, 2019

You don't call saga directly, you actually call store.dispatch(action).

I know it.

@aybb
Copy link

aybb commented Aug 21, 2019

You don't call saga directly, you actually call store.dispatch(action).

I know it.

Then what is your question about ? You can use something like https://github.com/Chion82/redux-wait-for-action this in order to achieve this behavior but you should not build your app in that way.

@kutlugsahin
Copy link

Hey @alanhg

I needed the same behavior once so I wrote my own helper functions to do that. It also enables waiting for the result of dispatched actions in saga. Take a look at redux-saga-callback.

@alanhg
Copy link
Author

alanhg commented Dec 28, 2019

@kutlugsahin I will try.

@nilu1991
Copy link

Hello @alanhg ,

Did you got any solution for this, if yes Can you please update?

@alanhg
Copy link
Author

alanhg commented Feb 2, 2020

@nilu1991 I found a solution,@adobe/redux-saga-promise

and I create demo, you can read my commit
alanhg/react-demo@52f7ef7

@alanhg alanhg closed this as completed Feb 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants