Given the following code, I am getting an undefined
result back from my yield call
. Why is this happening? I am thinking its something with my request function. Not sure why.
In my saga file, this line is coming back undefined:
const result = yield call(request, Routes.insightTotals);
I have verified the request is firing off and returning correctly. Could it be my promise chain that is causing this to be undefined?
HTTP Request functions
function request(url, options) {
return fetch(url, options)
.then(parseJSON)
.then(checkStatus);
}
function parseJSON(response) {
if (response.url === 'https://myurl.com') {
// This returns as a GZIP body so need to convert to text then parse as JSON
return response.text()
.then((res) => {
const parsed = JSON.parse(res);
console.log(parsed)
return parsed;
}).catch((err) => {
throw new Error(err);
});
}
return response.json();
}
Saga file
export function* getInsightTotalsRequestHandler() {
yield takeEvery(actions.GET_INSIGHT_TOTALS, function* getInsightTotalsRequest() {
try {
const result = yield call(request, Routes.insightTotals);
console.log(result); // THIS IS UNDEFINED
yield put({
type: actions.GET_INSIGHT_TOTALS_RETURN,
value: { result },
});
} catch (error) {
yield put({
type: actions.GET_INSIGHT_TOTALS_RETURN,
value: { error: error.message ? error.message : '' },
});
}
});
}
export default function* mySaga() {
yield all([
fork(other1RequestHandler),
fork(other2RequestHandler),
fork(getInsightTotalsRequestHandler),
]);
}
The call is an effect of Redux Saga basically we use a call to call any normal function in our generator functions. The advantage of using call in saga middleware is, it will not move to the next operation until and unless the call function gets resolved.
clicking the first button runs the saga with yield call and the second button will run using yield*. You can see in the console that it's not getting to the log for the yield call
The advantage of using call in saga middleware is, it will not move to the next operation until and unless the call function gets resolved. The majority of we use call side effects for our API calls, we write our API calls in our call functions.
after so many trials I found yield* can detect return type of function. so we can implement a custom version of each effect that must call with yield* and in that new custom function, we just handle type and then call real one with a yield. Sorry, something went wrong. @PejmanNik Well, I'm not sure to follow. Do you have a code example?
Yes, it is. call
will return the resolved promise value. My best guess is that checkStatus
does not return a value (You're not showing it).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With