[英]'redux-saga' not working asynchronously
我正在嘗試(學習)使用redux-saga發出異步發布請求,但是我正在獲得同步行為。
這是我正在使用的代碼:
import {AUTH_REQUEST} from '../constants/authentication';
import {authSuccess, authError} from '../actions/login';
import {takeEvery, call, put, fork, all} from 'redux-saga/effects';
import axios from 'axios';
const authenticate = (username, password) => {
axios
.post('http://localhost:8000/api/auth/login/', {username, password})
.then((response) => {
console.log('RESPONSE: ', response.data);
return response.data;
})
.catch((error) => {
throw error;
});
};
function* watchAuthRequest({username, password, resolve, reject}) {
try {
const result = yield call(authenticate, username, password);
console.log('RESULT', result);
yield put(authSuccess(result));
yield call(resolve);
} catch (error) {
yield put(authError(error));
yield call(reject, {serverError: 'Something bad happend !'});
}
}
const authSaga = function* authSaga() {
yield takeEvery(AUTH_REQUEST, watchAuthRequest);
};
export default function* rootSaga() {
yield all([
fork(authSaga),
]);
};
當我提交表單時(我正在使用redux-form),這就是為什么我進入控制台日志的原因:
RESULT: undefined
RESPONSE: Object {user: Object, token: "04a06266803c826ac3af3ffb65e0762ce909b07b2373c83b5a25f24611675e00"}
甚至authSuccess
動作都被分配了一個空的有效負載( result
)
我在這里做錯什么了嗎?
您缺少return
:
const authenticate = (username, password) => {
return axios
.post('http://localhost:8000/api/auth/login/', {username, password})
.then((response) => {
console.log('RESPONSE: ', response.data);
return response.data;
})
.catch((error) => {
throw error;
});
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.