Search code examples
javascriptreactjsreduxredux-sagazendesk

How do I use yield inside a callback on redux-saga?


I have a callback which I need to return true or false to trigger some external action, I need to make an API call inside this callback, so I need to get the state and dispatch some actions inside the callback, I don't know if I can use eventChannel because this callback could not be a generator, only a plain function. I need to do something like this.

  zafClient.on('ticket.save', () => {
      const state = yield select();
      yield put(actionWithApiCall())

      // I need to wait for the action to finish and then return something 
      // based on the response from the API
      // I know how to block the saga to wait for the action dispatched 
      // the problem is that I can't use yield here

      return somethingfromthestore;
  });

Btw, this is zendesk API.


Solution

  • Your not going to be able to pass a generator function to that API. The work around is to dispatch an action directly to the redux store and then write a saga that listens for that action.

    zafClient.on('ticket.save', () => reduxStore.dispatch(actionWithApiCall()))
    

    You will have to make the redux store exportable from where you create it. So that you can directly access it here.