3
최근에 redux-saga를 사용하기 시작했으며 정말 좋아합니다.redux thunk에서 api call wrapper를 redux saga로 마이그레이션하는 방법
약속 (내 API 호출)을 취하고 프리 로더를 표시하고 오류를 처리하는 API 호출에 다음과 같은 래퍼가 있습니다.
export const callApi = (promise: Promise<any>, errorMsg: string = 'Api error') => (dispatch: Dispatch) => {
dispatch(setLoading(true));
return promise.then(
(response) => {
dispatch(setLoading(false));
return response.body;
},
(error) => {
dispatch(setLoading(false));
dispatch(apiError(errorMsg, error));
return error;
});
};
나는 이러한 방식으로 redux saga에서 어떻게 동작합니까? 나는 이와 같은 일을하는 어떤 예도 찾을 수 없었다.
은 지금까지 나는
const camelizeKeysPromise = (obj) => Promise.resolve(camelizeKeys(obj));
export function* sagaCallApi(promise: Promise<any>, errorMsg: string = 'Api error') {
yield put(setLoading(true));
try {
const response = yield call(promise);
try {
const result = yield call(camelizeKeysPromise(response.body));
return result;
} catch (e) {
return response.body;
}
} catch (exception) {
yield put(setLoading(false));
yield put(apiError(errorMsg, error));
};
}
답변 해 주셔서 감사합니다. 방금 채널에서 읽었습니다. 약속 대신에 약속을 되 돌리는 함수를 전달하면 이것이 필요하지 않다고 생각하는 것이 옳은가요? – Tom
당신이 질문을 missinderstood 않는 한 당신은 약속을 얻을 것이기 때문에 당신은 아마 그 기능에 전화 효과를 양보하는 응답을하지 않기 때문에 같은 문제가 끝날거야. –