0
我正在使用可重用的生成器函數來調用REQUEST/SUCCESS/FAILURE操作。然後我有另一個生成器來調用該函數,但我想獲得某種反饋並提出另一個操作。不知道如何解釋它,這裏是我想要做的一個例子:Redux-saga使用可重複使用的生成器調度多個操作
/* in actions/index.js */
export const login = {
request:() => action(constants.LOGIN.REQUEST),
success: (response) => {
try {
jwtDecode(response.auth_token);
} catch (e) {
return action(constants.LOGIN.FAILURE,
{ payload: { error: {
status: 403,
statusText: 'Invalid token',
} } });
}
return action(constants.LOGIN.SUCCESS, { payload: { response } });
},
failure: error => action(constants.LOGIN.FAILURE, { payload: { error } }),
};
/* sagas/index.js */
function* postEntity(entity, apiFn, body) {
yield put(entity.request());
const { response, error } = yield apply(null, apiFn, body);
if (response) {
yield put(entity.success(response));
} else {
yield put(entity.failure(error));
}
}
function* postLogin(action) {
yield postEntity(login, api.login, [action.payload.email, action.payload.password]);
// How can I get some kind of feedback (succeed or not) from postEntity here and do a put(something_else) if succeeded?
}
export default function* rootSaga() {
yield takeLatest(constants.LOGIN_USER, postLogin);
}
任何反饋是非常感謝。
謝謝!
你能告訴我'postEntity'代碼怎麼會返回它嗎?我試過了,但我似乎無法讓它返回一些東西。 – aericson
只需返回回覆 –
哦,猜我的問題是正確理解生成器。謝謝! – aericson