Tony I.
Tony I.

Reputation: 632

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?

Upvotes: 1

Views: 219

Answers (1)

Aleksey L.
Aleksey L.

Reputation: 37938

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();

Upvotes: 1

Related Questions