Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在redux saga的回调中使用收益率?_Javascript_Reactjs_Redux_Redux Saga_Zendesk - Fatal编程技术网

Javascript 如何在redux saga的回调中使用收益率?

Javascript 如何在redux saga的回调中使用收益率?,javascript,reactjs,redux,redux-saga,zendesk,Javascript,Reactjs,Redux,Redux Saga,Zendesk,我有一个回调,我需要返回true或false来触发一些外部操作,我需要在这个回调中进行API调用,所以我需要获取状态并在回调中分派一些操作,我不知道我是否可以使用eventChannel,因为这个回调不能是生成器,只能是一个普通函数。我需要这样做 zafClient.on('ticket.save', () => { const state = yield select(); yield put(actionWithApiCall()) // I

我有一个回调,我需要返回true或false来触发一些外部操作,我需要在这个回调中进行API调用,所以我需要获取状态并在回调中分派一些操作,我不知道我是否可以使用eventChannel,因为这个回调不能是生成器,只能是一个普通函数。我需要这样做

  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;
  });

顺便说一句,这是zendesk API。

您将无法向该API传递生成器函数。解决方法是直接向redux商店发送一个动作,然后编写一个故事来监听该动作

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

您必须使redux存储可从创建它的位置导出。因此,您可以在这里直接访问它。

其中一个挑战是如何在回调中
产生
。导入
分派
怎么样?调度不是非生成器版本的
yield

如果您正在使用React,那么这样做似乎是一种选择

在用于指定回调的同一个过程中,您可以随后侦听在dispatch/action creator回调中创建的操作。这是通过指定另一个
watchFor
函数来实现的(在我的设置中,任何数量的导出
watchFor
函数都会添加到saga观察器池中)

配对的saga
worker
可以在使用最终操作创建者通过更新存储来“记录”工作流之前,完成对API调用返回的数据的任何需要

另一个选项可能是将api调用包装在
eventChannel
(请参阅:)中


-E

但是我如何等待API的响应在存储区内,以便根据更新的存储区返回一些内容?使用
yield take(我的API\u操作)
然后
yield select()
以新的方式从存储区获取数据