有没有办法使'端点。

发布于 2025-02-11 02:06:44 字数 733 浏览 2 评论 0原文

我派遣a rtkquery突变

const result = await dispatch(
  commentsApi.endpoints.myEndpoint.initiate(params)
)

注意到,当基础请求失败时,这不会抛出异常 - 上面的dispatch呼叫syolves将其带入具有error属性的对象。

这是什么原因?我更喜欢拒绝,所以我可以这样使用它:

try {
  await dispatch(commentsApi.endpoints.myEndpoint.initiate(params))
  someOtherCallDependentOnMutationSuccess()
} catch (e) {
  // ...
}

有没有办法使所有endpoint.initiate()调用呼叫拒绝在基础请求失败时拒绝?还是有充分的理由不想要?

I dispatch a RTKQuery mutation inside a custom thunk like this:

const result = await dispatch(
  commentsApi.endpoints.myEndpoint.initiate(params)
)

and I noticed that when the underlying request fails, this does not throw an exception - the above dispatch call resolves into an object with an error property instead.

What is the reason for that? I would prefer rejecting, so I could use it like this:

try {
  await dispatch(commentsApi.endpoints.myEndpoint.initiate(params))
  someOtherCallDependentOnMutationSuccess()
} catch (e) {
  // ...
}

Is there a way to make all endpoint.initiate() calls reject when the underlying request fails? Or is there a good reason not to want that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

窝囊感情。 2025-02-18 02:06:44

您可以更改逻辑,以使dispatch使用 .unwrap()

try {
  await dispatch(commentsApi.endpoints.myEndpoint.initiate(params)).unwrap()
  // following code will be reached only if previous dispatch succeded
  someOtherCallDependentOnMutationSuccess()
} catch (e) {
  // ...
}

You can change the logic to make the dispatch throw an error using .unwrap():

try {
  await dispatch(commentsApi.endpoints.myEndpoint.initiate(params)).unwrap()
  // following code will be reached only if previous dispatch succeded
  someOtherCallDependentOnMutationSuccess()
} catch (e) {
  // ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文