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

How to dispatch more actions in 1 yield via PUT #459

Closed
zatziky opened this issue Jul 27, 2016 · 10 comments
Closed

How to dispatch more actions in 1 yield via PUT #459

zatziky opened this issue Jul 27, 2016 · 10 comments

Comments

@zatziky
Copy link

zatziky commented Jul 27, 2016

Why is redux-saga not dispatching action when used in this form:

function* updatePorts(action) {
    const {response} = yield SagaEffects.call(apiUpdatePorts, action.ports);
    if (response) {
        yield (() => {
            console.log("Dispatching - ", action.actionAfter);
            SagaEffects.put(action.actionAfter);
        })();

But it works in this manner:

function* updatePorts(action) {
    const {response} = yield SagaEffects.call(apiUpdatePorts, action.ports);
    if (response) {
        yield SagaEffects.put(action.actionAfter);

I would like to dispatch 2 actions in 1 yield thats why I am trying to use the arrow function (() => {}).

@smashercosmo
Copy link

If you want to dispatch 2 actions in 1 yield, you can just use redux-batched-actions package.

@slorber
Copy link
Contributor

slorber commented Jul 27, 2016

you can yield an array of effects

@zatziky
Copy link
Author

zatziky commented Jul 28, 2016

⚠ see @kyle-ssg 's answer below. This one is depricated

@slorber It worked. I used it this way:

yield [
            SagaEffects.put(actions.saveItemsOk()),
            SagaEffects.put(actionsButton.savingEnded())
        ]

@zatziky zatziky closed this as completed Jul 28, 2016
@kyle-ssg
Copy link

This post ranks high in google for this question, so thought I'd mention the above is deprecated and that you now want to do:

yield SagaEffects.all([
            SagaEffects.put(actions.saveItemsOk()),
            SagaEffects.put(actionsButton.savingEnded())
        ])

@preciselywilliam
Copy link

preciselywilliam commented Apr 8, 2019

@kyle-ssg is there any technical difference between 2x yield put and yield all([put, put])?

@koobeeyak
Copy link

@preciselywilliam @ronayumik if you haven't seen it yet, in the docs for SagaEffects.all() they compare it to a close representation of Promise's all(), which returns a single promise once all the promises passed to it are resolved.
So if same idea applies to redux, you'll get one promise from yield all([put, put]), something you couldn't do with two separate yield puts.

@Absvep
Copy link

Absvep commented Jan 18, 2020

This post ranks high in google for this question, so thought I'd mention the above is deprecated and that you now want to do:

yield SagaEffects.all([
            SagaEffects.put(actions.saveItemsOk()),
            SagaEffects.put(actionsButton.savingEnded())
        ])

@kyle-ssg Great answer! But does this properly batch the two actions into a single subscription event? Making redux-batch redundant in this scenario.

@Andarist
Copy link
Member

@kyle-ssg Great answer! But does this properly batch the two actions into a single subscription event? Making redux-batch redundant in this scenario.

No. Even batch from Redux doesn't do that. It merely batches renders, not subscribers

@Absvep
Copy link

Absvep commented Jan 19, 2020 via email

@icharge
Copy link

icharge commented Apr 14, 2020

Ok, but https://github.com/manaflair/redux-batch/blob/master/README.md says that ” Note: Since this package got written, redux-saga improved and using all([ put(...), put(...) ]) seems to properly batch the two actions into a single subscription event, making redux-batch redundant in this scenario.” So should one use redux-batch with yield all([put(...),put(...)]) or it will not do batching? Or do I misunderstand something? I.e do I need to use redux-batch in configeStore.js or is simply using yield.all(put,put) enough?

 @kyle-ssg Great answer! But does this properly batch the two actions into a single subscription event? Making redux-batch redundant in this scenario. No. Even batch from Redux doesn't do that. It merely batches renders, not subscribers — You are receiving this because you commented. Reply to this email directly, view it on GitHub, or unsubscribe.

I just try and it work perfectly fine

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

9 participants