admin管理员组文章数量:1278910
I'm very new to redux-saga and am reading through the documentation. One of the things I'm not understanding is what, specifically, the API calls return.
When I say "return", I suppose I'm asking two things
what is the return value of what the documentation calls the "factory function"? (that is, here .html the docs state that "Since Sagas always yield an Effect, and these effects have simple factory functions (e.g. put, take etc.)", what, in general, would be the return values of these factory functions?
what is the return value of these "factory functions" when
yield
ed to the redux-saga middleware?
For instance, in the documentation (.html) they have
import {fork, take} from "redux-saga/effects"
const takeEvery = (pattern, saga, ...args) => fork(function*() {
while (true) {
const action = yield take(pattern)
yield fork(saga, ...args.concat(action))
}
})
I understand, from looking at it, that here take
when yield
ed returns the action
that matches the pattern. Thus I take that to always be the return value. So that here (.html)
function* loginFlow() {
while (true) {
const {user, password} = yield take('LOGIN_REQUEST')
const token = yield call(authorize, user, password)
if (token) {
yield call(Api.storeItem, {token})
yield take('LOGOUT')
yield call(Api.clearItem, 'token')
}
}
}
user
and password
are fields in the returned action?
But where is this documented, that take
, when yield
ed, returns the action? I went to the documentation for take
, and while there's a fairly good description of what it does, I didn't see what it returns.
In general, is there some underlying assumption about the API calls and their return values that I'm missing, due to my newbie status? Or -- and this is a distinct possibility -- I may simply have overlooked where it mentions the return value.
Thanks for any insights -- and if you know of a good alternative, thorough overview of redux-saga, I'd wele any links. I've been poring through tutorials and blog posts, but would love a good in-depth look.
I'm very new to redux-saga and am reading through the documentation. One of the things I'm not understanding is what, specifically, the API calls return.
When I say "return", I suppose I'm asking two things
what is the return value of what the documentation calls the "factory function"? (that is, here https://redux-saga.js/docs/advanced/Testing.html the docs state that "Since Sagas always yield an Effect, and these effects have simple factory functions (e.g. put, take etc.)", what, in general, would be the return values of these factory functions?
what is the return value of these "factory functions" when
yield
ed to the redux-saga middleware?
For instance, in the documentation (https://redux-saga.js/docs/advanced/Concurrency.html) they have
import {fork, take} from "redux-saga/effects"
const takeEvery = (pattern, saga, ...args) => fork(function*() {
while (true) {
const action = yield take(pattern)
yield fork(saga, ...args.concat(action))
}
})
I understand, from looking at it, that here take
when yield
ed returns the action
that matches the pattern. Thus I take that to always be the return value. So that here (https://redux-saga.js/docs/advanced/NonBlockingCalls.html)
function* loginFlow() {
while (true) {
const {user, password} = yield take('LOGIN_REQUEST')
const token = yield call(authorize, user, password)
if (token) {
yield call(Api.storeItem, {token})
yield take('LOGOUT')
yield call(Api.clearItem, 'token')
}
}
}
user
and password
are fields in the returned action?
But where is this documented, that take
, when yield
ed, returns the action? I went to the documentation for take
, and while there's a fairly good description of what it does, I didn't see what it returns.
In general, is there some underlying assumption about the API calls and their return values that I'm missing, due to my newbie status? Or -- and this is a distinct possibility -- I may simply have overlooked where it mentions the return value.
Thanks for any insights -- and if you know of a good alternative, thorough overview of redux-saga, I'd wele any links. I've been poring through tutorials and blog posts, but would love a good in-depth look.
Share Improve this question edited Nov 23, 2018 at 17:45 Cerulean asked Nov 23, 2018 at 17:32 CeruleanCerulean 6,01311 gold badges72 silver badges123 bronze badges2 Answers
Reset to default 6How redux-saga works
Ok, my take on how redux-saga works.
First of all, with redux-saga, you are writing using ES6 generator functions. A generator function works like a music box: as you turn the handle, it plays notes one by one. When you stop, it stops.
In our case, it's redux-saga runtime who turns the handle. The notes are the effects that your saga yields.
Generator functions that you write are pletely passive. They yield plain objects called effects. You can print an effect to see how it looks like:
console.log(take('SOME_ACTION'))
You'll see it's just an object. The take()
function just creates such an object, it has no side effects. If you forget to use yield
, for example, nothing will happen.
How take() works
Let's suppose you yield a take()
effect from your saga:
const action = yield take('SOME_ACTION')
When redux-saga runtime executes your saga, it does something like this:
// It initializes the generator
let gen = yourSaga()
// And then it turns the handle,
// extracting effects that you yield, one by one
while (true) {
// ...
const effect = gen.next().value
// Now redux-saga analyses your effect, and executes what you asked for
// ...
}
When it eventually stumbles on your take()
effect, it stops and puts off the generator for later.
Later, when redux-saga runtime detects that SOME_ACTION
was dispatched, it finds your generator and continues executing it:
gen.next(action).value
But this time redux-saga uses a generator feature that a music box doesn't have. Redux-saga runtime supplies the action that was detected via the next()
function. What you pass to next()
then bees the value of yield ...
expression inside your generator.
I hope this is helpful, because redux-saga docs frequently slip into talking about generators.
just looked into sagas for the first time.
as I understand it, take
is similar to a listener.
in your example a user is logged in and then taking
the first LOGOUT
that will happen.
in this case waiting for logout means, that you are waiting until the action LOGOUT is dispatched. So you will get the parameters that were dispatched with the logout.
the logout is probably dispatched as follows
put({type: 'LOGOUT', ...params})
params
is what you get when you are listening to LOGOUT
using take.
本文标签: javascriptWhat do reduxsaga API calls return (takefor example)Stack Overflow
版权声明:本文标题:javascript - What do redux-saga API calls return? (take, for example) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741240123a2363809.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论