rel1x rel1x - 9 months ago 64
React JSX Question

Redux-saga doesn't wait for response from API

I use redux-saga and I created a generator

which perform the API call. I though that
const username
will equal to response from API, but I've got

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
function, which call API
is equal
{"isExist": false}

checkUsername(username) {
fetch(url, {
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}


In your checkUsername function you need to return the call to fetch().