【问题标题】:Why does yield call(response.json) hang?为什么 yield call(response.json) 挂起?
【发布时间】:2021-05-27 03:15:02
【问题描述】:
const response = yield call(fetch, `${config.backendUrl}/verify`, {
  method: 'POST'
})

const responseJson = yield call(response.json)

console.log(responseJson)

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

【问题讨论】:

  • 也许call 正在传递导致response.json 采取不同行动的参数。试试(...args) => { console.log(args); response.json() } 看看他们是什么
  • args 似乎是空数组

标签: redux redux-saga yield


【解决方案1】:

这是因为当您调用 yield call(response.json) 时,response.json 被调用时使用了错误的上下文 (this)。

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

const responseJson = yield response.json();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-09-12
    • 2018-03-12
    • 1970-01-01
    • 1970-01-01
    • 2022-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多