我很难把这个任务取消。它在一个小循环中,如果我在函数底部的累加器中使用takeLatest,它就运行得很好。如果使用flowControl,则什么都不会发生/ take函数似乎不会运行。
cancel操作会触发,但是任务将继续运行--这是使用while的问题吗?这是我没看到的另一个问题吗?
如果问题是在使用页面底部的*appRoot导出此流时,如何将流控制正确地包含到React/Redux存储中?
function* install() {
const items = list.length - 1; // total number of things to call
let count = 1;
while (count < items) {
const response = yield call(() => Promise.resolve(list[count]));
if (response && !response.error) {
yield put({type: LOAD_ITEM_SUCCESS, item: response.data});
console.log('success, created new item', response.data);
}
yield delay(5000);
}
}
export function* flowControl() {
while ( true ) {
// starts the task in the background
const task = yield fork(install);
// wait for the user stop action
yield take(CANCEL_ACTION, cancelInstall, task);
}
}
function* cancelInstall(task){
yield cancel(task);
}
export default function* appRoot() {
yield all([
takeLatest(LOAD_ITEMS, flowControl)
]);
}更新其他人后橡胶鸭在这里:
flowControl中的while循环是不可伪造的,因此它永远运行。take不像预期的那样工作,但是用takeLatest替换它会正确触发取消函数。虽然出现在演示中,while循环没有一个可证伪的状态,所以它只是.跑啊。一群人!我不知道为什么,但是移除FlowControl,同时用takeLatest替换取消take使这个流像预期的那样工作。
发布于 2018-05-09 21:27:16
拿走只接收一个参数。
使用此更改重试:
export function* flowControl() {
while ( true ) {
// starts the task in the background
const task = yield fork(install);
// wait for the user stop action
yield take(CANCEL_ACTION); // <-
yield call(cancelInstall, task); // <-
}
}https://stackoverflow.com/questions/50261780
复制相似问题