首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Yield调用完成之前触发了yield

在Yield调用完成之前触发了yield
EN

Stack Overflow用户
提问于 2018-01-17 16:49:57
回答 1查看 127关注 0票数 0

我试图进行API调用,但在yield调用完成执行之前触发了yield put。下面是我的代码:

Api.js

代码语言:javascript
复制
function callApi(endpoint, token = null) {
  const fullUrl =
    endpoint.indexOf(API_ROOT) === -1 ? API_ROOT + endpoint : endpoint;

  return axios
    .get(fullUrl, { headers: { Authorization: token } })
    .then(resp => {
      return Object.assign([], resp.data);
    })
    .catch(error => ({ error: error.message || "Something bad happened" }));
}

export const checkOpenRegister = (branchId, userId, token) => {
  console.log("in check open");
  callApi(
    `Branches/${branchId}/registers?filter[where][status]=1&filter[where][userId]=${userId}`,
    token
  );
};

在我的传奇index.js

代码语言:javascript
复制
function* doCheckAuthInfo() {
  try {
    const user = yield select(getUser);

    if (user.token) {
      yield put({
        type: CHECK_AUTH_INFO_SUCCEED,
        payload: { token: user.token }
      });
      yield put({ type: CHECK_OPEN_REGISTER_REQUESTED });
    } else {
      //redirect to login
      yield put(NavigationActions.navigate({ routeName: "Login" }));
    }
  } catch (error) {
    yield put({ type: CHECK_AUTH_INFO_FAILED, error });
  }
}

function* doCheckOpenRegister() {
  try {
    const user = yield select(getUser);

    const response = yield call(
      checkOpenRegister,
      user.branchId,
      user.userId,
      user.token
    );
    yield put({ type: CHECK_OPEN_REGISTER_SUCCEED, payload: response });
  } catch (error) {
    yield put({ type: CHECK_OPEN_REGISTER_FAILED, error: error.message });
  }
}

function* watchCheckAuthInfo() {
  yield takeLatest(CHECK_AUTH_INFO_REQUESTED, doCheckAuthInfo);
}

function* watchCheckOpenRegister() {
  yield takeLatest(CHECK_OPEN_REGISTER_REQUESTED, doCheckOpenRegister);
}

// use them in parallel
export default function* rootSaga() {
  yield all([
    fork(watchCheckAuthInfo),
    fork(watchCheckOpenRegister)
  ]);
}

在我的传奇中,在函数doCheckOpenRegister上,yield没有有效负载,但我可以在我的网络调试器中找到有效负载。

我需要有效载荷来触发重定向操作。在这种情况下,当响应中有一个值时,我需要重定向到主页。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-01-17 17:58:16

您忘记了函数checkOpenRegister的返回承诺

代码语言:javascript
复制
export const checkOpenRegister = (branchId, userId, token) => {
  console.log("in check open");
  return callApi(
    `Branches/${branchId}/registers?filter[where][status]=1&filter[where][userId]=${userId}`,
    token
  );
};

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48296992

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档