redux - Redux Saga - 为什么 yield all 不等待 put 操作完成后再继续
问题描述
我想等待我的 yieldall 中的所有 put 操作完成,然后再继续下一个 yield。我认为这就是 yieldall 应该达到的目标。我是 Redux Saga 的新手,因此我可能遗漏了一些细节或逻辑。
这是我的workerAnalytics(第一个使用调度'ANALYTICS'调用的工人)
export function* workerAnalytics(action) {
console.log('here')
let group = action.group
yield put({ type: 'SHOW_LOADER', loading: action.loading })
yield all([
put({ type: 'BASIC_ANALYTICS', loading: 'client', group: group }),
put({ type: 'BASIC_ANALYTICS', loading: 'KPI', group: group }),
put({ type: 'BASIC_ANALYTICS', loading: 'country', group: group }),
put({ type: 'BASIC_ANALYTICS', loading: 'estimated_revenue' }),
])
console.log('there')
yield put({ type: 'HIDE_LOADER', loading: action.loading })
}
我的workerBasicAnalytics(在从调度类型'BASIC_ANALYTICS'调用watcherBasicAnalytics之后调用)
export function* workerBasicAnalytics(action) {
try {
let data;
if (action.loading === 'KPI') { yield put({ type: 'SHOW_LOADER', loading: action.loading }) }
data = action.loading === 'client' ? yield call(() => axiosInstance.get(`/customer-conversion/?group=${action.group}`)) :
action.loading === 'KPI' ? yield call(() => axiosInstance.get(`/kpi/?group=${action.group}`)) :
action.loading === 'country' ? yield call(() => axiosInstance.get(`/customer-country/?group=${action.group}`)) :
action.loading === 'estimated_revenue' ? yield call(() => axiosInstance.get('/estimated-revenue/')) : null
yield put({ type: "STORE_DATA", payload: data.data, fetch: action.loading })
if (action.loading === 'KPI') { yield put({ type: 'HIDE_LOADER', loading: action.loading }) }
console.log(action.loading)
} catch (error) {
console.log(error)
}
}
正如你所看到的,我在我的工作函数中都有 console.log 来跟踪哪个是首先执行的。问题是 'there' 是在 'here' 之后记录的,而它应该是在记录 'there' 之前应该记录的单个 workerBasicAnalytics console.log(action.loading),如 console.log( 'there') 出现在 yieldall 之后。
感谢所有帮助,谢谢!
解决方案
您是否检查过是否调用了 workerBasicAnalytics?或者,如果您在 catch 处理程序中发现了错误?
更新: 你可以这样做:
yield all([
workerBasicAnalytics({loading: 'client', group: group}),
workerBasicAnalytics({loading: 'KPI', group: group}
)]
推荐阅读
- ios - 如何在 Swift 4.1 中将 & 编码为 %26
- php - 表单更新数据库模型记录的关系
- vba - 如何在visual basic中查找并突出显示richtextbox中的下一个单词实例?
- java - org.json JSONObject 在从 Spring Boot 控制器返回时向 JSONObject 添加额外的对象
- mysql - 如何使用mysql从字符串中删除双引号
- java - 从 url 显示黑色顶部和底部加载图像
- php - 从 Yii1 中的 session 公共函数 search() 中获取 ID
- svn - 在现有主干中解耦大小代码并使用当前代码创建新主干的最佳方法
- xslt - XSL 转换返回空白值
- python - 根据第二列的值在 csv 中选择一个项目