你能调用一个 saga 并仅在另一个 saga 完成后才继续执行吗?
Can you call a saga and continue execution only when a different saga is finished?
我们的应用程序使用尝试 - 成功 - 失败的方法来处理来自服务器的响应。
我有一个生成器函数需要像这样运行:
function * getSingleSectorAttempt(action) {
const sectors = yield select(getSectors);
if (!sectors) {
//If there are no sectors, I need to call the GET_SECTORS_ATTEMPT action
//and only restart/continue this saga when the GET_SECTORS_SUCCESS action is fired
}
const id = sectors[action.name].id;
try {
const response = yield call(api.getSector, id);
//...
} catch (err) {
//...
}
}
根据我对 Redux Saga 文档的阅读,这似乎无法立即实现。但是,我想看看我是否遗漏了什么。我已经试过了:
yield fork(takeLatest, Type.GET_SECTORS_SUCCESS, getSingleSectorAttempt);
yield put(Actions.getSectorsAttempt());
在 if(!sectors)
条件块中,但是虽然这有效,但它不会保留初始的 GET_SINGLE_SECTOR_ATTEMPT
操作参数,而且我不确定如何在不进入回调的情况下让它这样做,并且争论意大利面。
哎呀,明白了:
function* getSingleSectorAttempt(action) {
const sectors = yield select(getSectors);
if(!sectors){
//Pass initial action in a callback function like so:
yield fork(takeLatest, Type.GET_SECTORS_SUCCESS, () => getSingleSectorAttempt(action));
yield put(Actions.getSectorsAttempt());
} else {
const id = sectors[action.name].id;
try {
const response = yield call(api.getSector, id);
//...
} catch (err) {
//..
}
}
}
让您等待动作被分派的效果是take
。在你的情况下:
function* getSingleSectorAttempt(action) {
let sectors = yield select(getSectors);
if (!sectors) {
yield put(getSectorsAttempt());
yield take(GET_SECTORS_SUCCESS);
sectors = yield select(getSectors);
}
// resume here as normal
}
您自己的回答可能会产生意想不到的副作用。例如,如果 getSectors
可以在应用程序的生命周期中多次 return 一个虚假值,您将有多个分叉进程等待 GET_SECTORS_SUCCESS
被调度,并且每个进程都执行您的 -效果,每个都保留对触发它的操作的引用。
我们的应用程序使用尝试 - 成功 - 失败的方法来处理来自服务器的响应。
我有一个生成器函数需要像这样运行:
function * getSingleSectorAttempt(action) {
const sectors = yield select(getSectors);
if (!sectors) {
//If there are no sectors, I need to call the GET_SECTORS_ATTEMPT action
//and only restart/continue this saga when the GET_SECTORS_SUCCESS action is fired
}
const id = sectors[action.name].id;
try {
const response = yield call(api.getSector, id);
//...
} catch (err) {
//...
}
}
根据我对 Redux Saga 文档的阅读,这似乎无法立即实现。但是,我想看看我是否遗漏了什么。我已经试过了:
yield fork(takeLatest, Type.GET_SECTORS_SUCCESS, getSingleSectorAttempt);
yield put(Actions.getSectorsAttempt());
在 if(!sectors)
条件块中,但是虽然这有效,但它不会保留初始的 GET_SINGLE_SECTOR_ATTEMPT
操作参数,而且我不确定如何在不进入回调的情况下让它这样做,并且争论意大利面。
哎呀,明白了:
function* getSingleSectorAttempt(action) {
const sectors = yield select(getSectors);
if(!sectors){
//Pass initial action in a callback function like so:
yield fork(takeLatest, Type.GET_SECTORS_SUCCESS, () => getSingleSectorAttempt(action));
yield put(Actions.getSectorsAttempt());
} else {
const id = sectors[action.name].id;
try {
const response = yield call(api.getSector, id);
//...
} catch (err) {
//..
}
}
}
让您等待动作被分派的效果是take
。在你的情况下:
function* getSingleSectorAttempt(action) {
let sectors = yield select(getSectors);
if (!sectors) {
yield put(getSectorsAttempt());
yield take(GET_SECTORS_SUCCESS);
sectors = yield select(getSectors);
}
// resume here as normal
}
您自己的回答可能会产生意想不到的副作用。例如,如果 getSectors
可以在应用程序的生命周期中多次 return 一个虚假值,您将有多个分叉进程等待 GET_SECTORS_SUCCESS
被调度,并且每个进程都执行您的 -效果,每个都保留对触发它的操作的引用。