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

Why do we have to use yield for 'put'-effect? #1669

Closed
c-goettert opened this issue Nov 7, 2018 · 7 comments
Closed

Why do we have to use yield for 'put'-effect? #1669

c-goettert opened this issue Nov 7, 2018 · 7 comments
Labels

Comments

@c-goettert
Copy link

I have two questions of understanding related to the yield put(someAction)-syntax (I suppose they are very simple questions, but I couldn't find an exact answer in the documentation).

1. Why do we have to yield when using put?
If my understanding is right, put-effect will only dispatch an action. Dispatching an action on a redux-store happens fully synchronous. Why do we need to use yield then? Is it just because every redux-saga effect is built up this way, or am I missing out some async-stuff happening which we need to wait for?

2. Is it safe to assume, that the new store is ready after yield put(...)?
Considering the following syntax:

yield put(action1)
yield put(action2)

Is it safe to assume that reducers will always work off action1 before action2 (in other words: action2 always works with the store previously updated from action1)?

Thank you very much for your help.

@khaliu
Copy link

khaliu commented Nov 8, 2018

Hi there! From my understandings:

1 - Remember that put is an effect that tells the middleware to dispatch the real action, as a list of instructions. You can call the real dispatch from the generator, but it isn't declarative and it doesn't gives you any benefits for testing purposes.

2 - I'm not really sure if you make parallel work dispatching both actions, but making a put in that way, calls ultimately the dispatch, that runs synchronously (redux).

@c-goettert
Copy link
Author

c-goettert commented Nov 9, 2018

Thanks for your reply!
Regarding to Question 2: If I understand you right, the answer to my question yes? (the order of dispatches will be called in the given order)?

@khaliu
Copy link

khaliu commented Nov 9, 2018

Yup, if you put a list of puts when the execution reaches the dispatches it will be executed synchronously

@Andarist
Copy link
Member

While it's true that put is a dispatch this is slightly more complex. To make various fork+take+put scenarios work we use a simple scheduler under the hood which queues the "nested" puts - that whole queue gets processed within single stack frame, but it means that a single put might not be executed right away.

  1. Is it safe to assume, that the new store is ready after yield put(...)?

It is safe to assume that.

@c-goettert
Copy link
Author

Thanks for the answers!

@miaozilong
Copy link

you can use yield yield put()

@Pragatigg
Copy link

It's may be the case, whatever action you want to dispatch may have watcher to run some other saga. if you wont place yield before put both generator will run asynchronously and outcome wouldn't be as expected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants