我使用 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
函数中,调用 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));
},
为什么我的 const username
不等于 {"isExist": false}
?
最佳答案
在您的 checkUsername
函数中,您需要返回对 fetch()
的调用。
关于javascript - Redux-saga 不等待 API 的响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39603017/