佐贺解决第一个动作后如何执行动作?

时间:2019-08-27 11:06:00

标签: javascript reactjs redux-saga

我正在研究一个使用React,Redux-Saga的项目。在我的代码中,我必须两次调用相同的动作。我要一个接一个地执行它们。但是发生的是,由于它们本质上是非阻塞的,因此它们立即被调用,并且在传奇选择器中,它仅解决到达它的第一个动作。如何从调用这些动作的地方在我的React组件中更改此行为?我无法在英雄传奇中进行更改,因为我不够了解它,而我只是在处理反应。 请指导我!

我尝试使用setTimeout(),但愚蠢的我无法使用,因为这些函数本质上是非阻塞的。

let body = {}

body = {
  uuid: dragRow.uuid,
  orderId: hoverRowId
}
console.log("update for: ", dragRow)
this.props.updateSection(body);


body = {
  uuid: hoverRow.uuid,
  orderId: dragRowId
}
console.log("update for: ", hoverRow);
this.props.updateSection(body);

这是传奇的代码:

//UPDATE SECTION
function* updateSection(sectionAction) {
try {
    const payload = yield call(Api.updateSection, sectionAction.payload);
    yield put({ type: sectionAction.type + "_success",payload: payload });
} catch (error) {
    yield put({ type: sectionAction.type + "_failed", error })
    }
}

function* watchUpdateSection() {
    yield takeLeading([sectionConstants.UPDATE_SECTION, sectionConstants.HIDE_SECTION, sectionConstants.PUBLISH_SECTION], updateSection)
}

预期的是,只有对我的第二次调用传递后,对updateSection()的第一个函数调用才能解析。现在,他们两个都试图一次执行,只有前一个执行。

1 个答案:

答案 0 :(得分:1)

对于同步执行,您可以使用actionChannel:https://redux-saga.js.org/docs/advanced/Channels.html

import { take, actionChannel, call, ... } from 'redux-saga/effects'

function* watchUpdateSection() {
  // create a channel that is buffered with actions of these types
  const chan = yield actionChannel([
    sectionConstants.UPDATE_SECTION,
    sectionConstants.HIDE_SECTION,
    sectionConstants.PUBLISH_SECTION,
  ]);
  while (true) {
    // take from the channel and call the `updateSection` saga, waiting
    // for execution to complete before taking from the channel again
    const action = yield take(chan);
    yield call(updateSection, action);
  }
}