Javascript 即使API失败,我如何运行代码
我有一个调用传奇的动作(在这里我调用了API)。该操作在循环中被调用两次 index.jsJavascript 即使API失败,我如何运行代码,javascript,reactjs,redux-saga,Javascript,Reactjs,Redux Saga,我有一个调用传奇的动作(在这里我调用了API)。该操作在循环中被调用两次 index.js componentDidMount(){ this.props.nameList.map((name) => { this.props.actions.getDetails(this.props.store.requestBody[name], name); }) } export const getDetails= (da
componentDidMount(){
this.props.nameList.map((name) => {
this.props.actions.getDetails(this.props.store.requestBody[name], name);
})
}
export const getDetails= (data, name) => ({
type: GET_DETAILS,
...{data, name}
})
export function* getDetails({data, name}){
try{
const response = yield call(getDetailsApi, data);
if (response) {
yield put(setDetails({[name] : response.data}));
}
}
catch(error){
yield put(setDetails({[name] : ''}));
}
}
export default function* rootSaga() {
yield takeLatest(GET_DETAILS, getDetails);
}
action.js
componentDidMount(){
this.props.nameList.map((name) => {
this.props.actions.getDetails(this.props.store.requestBody[name], name);
})
}
export const getDetails= (data, name) => ({
type: GET_DETAILS,
...{data, name}
})
export function* getDetails({data, name}){
try{
const response = yield call(getDetailsApi, data);
if (response) {
yield put(setDetails({[name] : response.data}));
}
}
catch(error){
yield put(setDetails({[name] : ''}));
}
}
export default function* rootSaga() {
yield takeLatest(GET_DETAILS, getDetails);
}
saga.js
componentDidMount(){
this.props.nameList.map((name) => {
this.props.actions.getDetails(this.props.store.requestBody[name], name);
})
}
export const getDetails= (data, name) => ({
type: GET_DETAILS,
...{data, name}
})
export function* getDetails({data, name}){
try{
const response = yield call(getDetailsApi, data);
if (response) {
yield put(setDetails({[name] : response.data}));
}
}
catch(error){
yield put(setDetails({[name] : ''}));
}
}
export default function* rootSaga() {
yield takeLatest(GET_DETAILS, getDetails);
}
我在这里尝试的是,如果try成功,则使用response更新reducer,如果失败,则使用“”(catch块中的代码)更新reducer
问题是,如果两个API调用都失败了(有时会失败),那么catch()只调用一次(对于第一个API调用失败)
如何在每次API失败时更新我的reducer?看起来问题是由帮助程序fn takeLatest()引起的。把它改成takeEvery()对我来说很有用
Unlike takeEvery, takeLatest allows only one fetchData task to run at any moment. And it will be the latest started task. If a previous task is still running when another fetchData task is started, the previous task will be automatically cancelled.
这看起来不仅仅是JavaScript。您能否显示捕获操作的代码,然后调用此
getDetails
函数?