首页 > 解决方案 > 为什么 yield call(response.json) 挂起?

问题描述

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

const responseJson = yield call(response.json)

console.log(responseJson)

这是来自 redux-saga 的代码。Yield 挂起并且console.log不打印任何内容。但是,如果我用它替换response.json它就() => response.json()可以了。为什么?

标签: reduxredux-sagayield

解决方案


那是因为当您使用错误的上下文 ( ) 调用yield call(response.json)时。response.jsonthis

您可以使用bind(eg yield call(response.json.bind(response))) 或指定context(eg yield call([response, response.json])) 来解决此问题,但call这里真的没用。你可以:

const responseJson = yield response.json();

推荐阅读