I have a saga makes multiple call in it:
function* membersInfoSaga({ payload }) {
const endpoints = [
api.fetchNotifications,
api.fetchPosts,
api.fetchMessages,
api.fetchActivities
]
const responses = yield all(endpoints.map(endpoint => {
try {
return call(endpoint, payload.memberId, payload.page, payload.pageSize)
} catch (error) {
return { data: {} }
}
}))
When a call gets 400 as a response, i want it to continue for the others and return { data: {} } for the failed call.
I am making calls with Fetch API. And here is my api.js:
function callApi(endpoint, options, unauthorized, isRefreshToken) {
const URL = BASE_URL + endpoint
const ACCESS_TOKEN = localStorage.getItem("access_token")
if (!options) options = {}
options["headers"] = {
"Content-Type": "application/json",
"Accept": "application/json"
}
if (!unauthorized) {
options.headers["Authorization"] = `Bearer ${ACCESS_TOKEN}`
}
return fetch(URL, options)
.then((response) => response.json())
.then((response) => {
if (response.code >= 200 && response.code <= 299) {
return Promise.resolve(response)
} else if (response.code === 401) {
if (isRefreshToken) {
store.dispatch({ type: actionTypes.AUTH_REFRESH_FAILED })
} else {
store.dispatch({
type: actionTypes.INVALID_TOKEN,
payload: { error: response.error.message }
})
return { error: response.error.message }
}
} else {
return Promise.reject(response.error.message)
}
})
.catch((error) => {
return Promise.reject(error)
})
}
And this is the function that i invoke inside saga call method:
const fetchNotifications = (memberId, page, pageSize) => callApi(`notifications/${memberId}?page=${page}&page_size=${pageSize}`)