Redux-saga 不等待 API 的响应

Redux-saga doesn't wait for response from API

我使用 redux-saga 并创建了一个生成器 checkUsername 来执行 API 调用。我虽然 const username 将等于来自 API 的响应,但我有 undefined

function* checkUsername(action) {
  try {
    const username = yield call(Api.checkUsername, action.username);
    yield put({type: actions.USERNAME_CHECK_SUCCEEDED, username});
  } catch (e) {
    yield put({type: actions.USERNAME_CHECK_FAILED, message: e.message});
  }
}

虽然在我的checkUsername函数中,调用APIres等于{"isExist": false}:

 checkUsername(username) {
    fetch(url, {
    'GET',
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json',
    }
  }).then(response => response.json())
      .then(res => res)
      .catch(error => console.error(error));
 },

为什么我的const username不等于{"isExist": false}

在您的 checkUsername 函数中,您需要 return 调用 fetch()