Redux-saga: How To Ignore One Error And Get Other Responses In Parallel Tasks?
Here is my code, fetch several reports in parallel : function fetchSingleReportRequest(reportId) { return axios.get(`/api/${reportId}`) } function* fetchReportsInfo({payload: {
Solution 1:
Descend your try-catch logic down into the anonymous function. That way you can define what to do each time a call fails. Here for example, I just return null
on a fail here.
functionfetchSingleReportRequest(reportId) {
return axios.get(`/api/${reportId}`)
}
function* fetchReportsInfo({payload: {data: reportIds}}) {
const responses = yieldall(reportIds.map(reportId => {
try {
returncall(fetchSingleReportRequest, reportId)
} catch (e) {
returnnull;
}
}));
}
Solution 2:
This happens because you are not handling the error in the Promise itself.
You just need to add a catch
block to the axios
request inside fetchSingleReportRequest
.
For example, you can write something like:
functionfetchSingleReportRequest(reportId) {
return axios.get(`/api/${reportId}`)
.catch(() => {
returnnull
})
}
function* fetchReportsInfo({payload: {data: reportIds}}) {
try {
let responses = yieldall(reportIds.map(reportId =>call(fetchSingleReportRequest, reportId)))
responses = responses.filter((res) => res !== null)
} catch (e) {
}
}
Post a Comment for "Redux-saga: How To Ignore One Error And Get Other Responses In Parallel Tasks?"