Search code examples
reduxredux-sagayield

Why does yield call(response.json) hang?


const response = yield call(fetch, `${config.backendUrl}/verify`, {
  method: 'POST'
})

const responseJson = yield call(response.json)

console.log(responseJson)

This is code from redux-saga. Yield hangs and console.log does not print anything. But if I replace response.json with () => response.json() it works. Why?


Solution

  • That's because when you invoke yield call(response.json) the response.json is called with wrong context (this).

    You could fix this with bind (e.g. yield call(response.json.bind(response))), or by specifying context (e.g. yield call([response, response.json])), but call here is really useless. You can just:

    const responseJson = yield response.json();