这是我的代码,并行获取多个报告:
function fetchSingleReportRequest(reportId) {
return axios.get(`/api/${reportId}`)
}
function* fetchReportsInfo({payload: {data: reportIds}}) {
try {
const responses = yield all(reportIds.map(reportId =>
call(fetchSingleReportRequest, reportId)))
} catch (e) {
}
}
然而,一个或多个报告可能不存在,但这并不影响结果,该错误可以被忽略。
但是当发生 404 fetch 时,它进入 catch 块,我怎样才能获得其他成功的响应?
将您的 try-catch 逻辑下降到匿名函数中。这样您就可以定义每次调用失败时要执行的操作。例如,我只是返回null
在这里失败了。
function fetchSingleReportRequest(reportId) {
return axios.get(`/api/${reportId}`)
}
function* fetchReportsInfo({payload: {data: reportIds}}) {
const responses = yield all(reportIds.map(reportId => {
try {
return call(fetchSingleReportRequest, reportId)
} catch (e) {
return null;
}
}));
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)