Redux Saga - 为什么 yield all 不等待 put 操作完成后再继续
Redux Saga - Why does yield all not wait for the put action to complete before proceeding
我想等待我的 yieldall 中的所有看跌操作完成,然后再继续下一个收益。我认为这就是 yieldall 应该实现的目标。我是 Redux Saga 的新手,因此可能会遗漏一些细节或逻辑。
这是我的 workerAnalytics(第一个使用 dispatch 调用的 worker '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?或者,如果您在捕获处理程序中发现了错误?
更新:
你可以这样做:
yield all([
workerBasicAnalytics({loading: 'client', group: group}),
workerBasicAnalytics({loading: 'KPI', group: group}
)]
我想等待我的 yieldall 中的所有看跌操作完成,然后再继续下一个收益。我认为这就是 yieldall 应该实现的目标。我是 Redux Saga 的新手,因此可能会遗漏一些细节或逻辑。
这是我的 workerAnalytics(第一个使用 dispatch 调用的 worker '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?或者,如果您在捕获处理程序中发现了错误?
更新: 你可以这样做:
yield all([
workerBasicAnalytics({loading: 'client', group: group}),
workerBasicAnalytics({loading: 'KPI', group: group}
)]