简体   繁体   中英

Redux-saga doesn't wait for response from API

I use redux-saga and I created a generator checkUsername which perform the API call. I though that const username will equal to response from API, but I've got 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});
  }
}

Although in my checkUsername function, which call API res is equal {"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));
 },

Why my const username is not equal {"isExist": false} ?

在您的checkUsername函数中,您需要将调用返回给fetch()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM