Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redux-Saga yield call returning back as undefined

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),
  ]);
}
like image 301
The Nomad Avatar asked Feb 05 '18 06:02

The Nomad


People also ask

What is the use of call in Redux Saga?

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.

How to run saga with yield call?

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

What is the advantage of using call in Saga middleware?

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.

Can yield* detect return type of function?

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?


1 Answers

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).

like image 152
Dennie de Lange Avatar answered Oct 12 '22 22:10

Dennie de Lange