Redux-saga不等待API的响应

rel*_*l1x 5 javascript reactjs react-native redux redux-saga

我使用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});
  }
}
Run Code Online (Sandbox Code Playgroud)

虽然在我的checkUsername函数中,哪个调用API res是相同的{"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));
 },
Run Code Online (Sandbox Code Playgroud)

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

idb*_*old 5

在您的checkUsername功能中,您需要将呼叫返回fetch().