简体   繁体   English

是否可以使用带有 createAsyncThunk 的外部 axios 处理程序

[英]Is it possible to use an external axios handler with createAsyncThunk

I have a API handler with different functions using Axios for get, post, patch, etc. I want to use those API handler with createAsyncThunk from redux toolkit, is this possible?我有一个具有不同功能的 API 处理程序,使用 Axios 进行获取、发布、补丁等。我想将这些 API 处理程序与 redux 工具包中的 createAsyncThunk 一起使用,这可能吗?

it would be something like它会像

export const paymentMethods = createAsyncThunk(
  'tenants/payments',
  async (ppp: any, { dispatch, getState }) => {
    const x = await postRequest(
      `${API.TENANTS}/${ppp.tenantId}/${API.PAYMENT_METHODS}`,
      response => console.log(response),
      ppp.data,
    );
    return x.data;
  },
);

The thing here is that createAsyncThunk handles rejected, fulfilled and pending response promises but if I use this configuration it will always return fulfilled even though the call actually fails.这里的问题是 createAsyncThunk 处理被拒绝、已完成和待处理的响应承诺,但如果我使用此配置,即使调用实际上失败,它也会始终返回已完成。

You can use unwrap function to make createAsyncThunk return errors in the catch statement.您可以使用unwrap函数使createAsyncThunkcatch语句中返回错误。 Check the following example.检查以下示例。

Without unwrap() :没有unwrap()

  dispatch(paymentMethods())
    // By default, dispatching asyncThunks never results in errors (errors are just saved at redux)
    .then((result) => {
      /*
        Here you can access the result (success or error) of the asyncThunk.
        If we don't use 'unwrap', error (if happens) will be returned here as:
        {
          error: {name: "AxiosError", message: "Request failed with status code 401", code: "ERR_BAD_REQUEST"}
          meta: {arg: {…}, requestId: "3eIa5l0L3J12ADFrkxirt", rejectedWithValue: false, requestStatus: "rejected", aborted: false, …}
          payload: undefined
          type: "tenants/payments/rejected"
        }
      */
      console.log(`PaymentMethods result (success or error): `, result)
    })
    .catch((error) => {
      // This WILL NEVER be reached
    })

With unwrap() :使用unwrap()

  dispatch(paymentMethods())
    // Using 'unwrap', the internal logic of the reducer doesnt change. 
    // But you can access the error in the 'catch' statement. 
    .unwrap()
    .then((result) => {
      // Here you can access the success result of the asyncThunk.
      console.log(`PaymentMethods result (success): `, result)
    })
    .catch((error) => {
      /*
        Here you can access the error result of the asyncThunk. Example:
        {
          code: "ERR_BAD_REQUEST"
          message: "Request failed with status code 401"
          name: "AxiosError"
        }
      */
      console.log(`PaymentMethods result (error): `, error)
    })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 axios 时如何在 createAsyncThunk 中创建播放负载 - How to create playload in createAsyncThunk when using axios 无法在Botpress(axios)上使用外部API - Unable to use external API on Botpress (axios) 是否可以为每个承诺使用带有 then() 的 axios.all ? - Is it possible to use axios.all with a then() for each promise? 如何在 JQuery 的事件处理程序中使用外部 function - How to use an external function in event handler in JQuery 是否可以在 Twilio 函数中使用 Axios ? 如果是这样,怎么做? - Is it possible to use Axios in Twilio Functions? If so, how? 有哪些常见的 Redux Toolkit 的 CreateAsyncThunk 用例 - What are some common Redux Toolkit's CreateAsyncThunk use cases 当我使用 createAsyncThunk 时,action.payload 返回未定义 - action.payload is returning undefined when I use createAsyncThunk 保存点击处理程序中的值,并在多个外部JavaScript文件中使用 - Save the value from a click handler and use in multiple external JavaScript files Javascript - 是否可以使用具有特定属性路径的 fetch 或 axios.get? - Javascript - Is it possible to use fetch or axios.get with a specific property path? 是否可以在dialogflow内联编辑器中使用外部库? - Is it possible to use external libraries in the dialogflow inline editor?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM