Cole Cole - 3 months ago 59
Javascript Question

Redux-Saga connect multiple generators to store

This may be fairly basic but I haven't found an solution. I'm using redux-saga to handle my async API calls with redux. I need to connect multiple listeners to the store. I tried

fork
like in the examples, and the first fork works, but the second one doesn't. How can I connect multiple listeners?

export default function* mySaga() {
yield [
fork(yield* takeEvery("FRIEND_FETCH_REQUESTED", fetchFriends)), // works
fork(yield* takeEvery("CREATE_USER_REQUESTED", createUser)), // doesn't work
]
}


Thanks!

Answer

This is because fork takes a function that returns a promise or a generator function as per the documentation here:

http://yelouafi.github.io/redux-saga/docs/api/index.html#forkfn-args

To make your situation work, just put the yield* in a generator function and it should all work :

export default function* mySaga() {
  yield [
    fork(watchFetchFriends),
    fork(watchCreateUser)
  ]
}
function watchFetchFriends() {
   yield* takeEvery("FRIEND_FETCH_REQUESTED", fetchFriends);
}
function watchCreateUser() {
    yield* takeEvery("CREATE_USER_REQUESTED", createUser);
}

Btw, I am not sure how your first yield* works though.

Comments