react-redux中意外的保留字'yield'

问题描述 投票:0回答:1

在我的react项目中,包括react-redux,我试图在[c0]请求的catch错误函数中使用yield,如下所示:

axios

但是由于catch错误函数不是生成器,因此出现此错误:

解析错误:意外的保留字'yield'

所以我试图将错误函数更改为生成器:

const result = yield axios.put(REQUEST_API, data, config)
  .catch(error => {
    yield put({ type: SET_MESSAGE, message: `ERROR_HAPPENED: ${error.response.data.status}` });
  });

但是现在我从const result = yield axios.put(REQUEST_API, data, config) .catch(function* (error) { yield put({ type: SET_MESSAGE, message: `ERROR_HAPPENED: ${error.response.data.status}); }); 得到此警告:

意外的未命名生成器函数。

我不确定这种方法有什么问题,但是这种方式eslint永远不会发现错误!

因此,如果axios请求中发生错误,是否有更好的实践来调度带有yield put的操作?

reactjs react-redux axios yield
1个回答
0
投票

[axios可以在yield的上下文中直接使用,在这里,您可以在包装的promise捕获中使用它,它带有自己的词法上下文。

正确的方法是:

Generators

如果做出承诺要在try块中等待的事实,则诺言的捕获将自动落入try块的捕获中。

希望这会有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.