简体   繁体   English

当第一次在 React 中使用 Axios 没有错误时进行第二次 api 调用

[英]Make second api call when there is no error on the first using Axios in React

I have three API calls which should be dependent on one another.我有三个 API 调用,它们应该相互依赖。 The second API call should trigger only when the first succeeds.第二个 API 调用应仅在第一个成功时触发。

With my current implementation, I'm getting a CORS error when the first API call is made and was able to catch the error in the catch block.在我当前的实现中,当第一次调用 API 并且能够在catch块中catch错误时,我收到了 CORS 错误。 However, I'm seeing that the second and third APIs calls are made irrespective of the error that got caught in the first API call.但是,我发现无论在第一个API 调用中捕获到的错误如何,都会进行第二个第三个API 调用。

Could anyone please advise?任何人都可以请指教吗?

const firstApiCall = async() => {
  try {
    await axios.post(
      process.env.FIRST_API,
      payload
    );
  ]
} catch (err) {
  console.log(`err`, err);
}
};


const secondApiCall = async() => {
  try {
    await axios.post(
      process.env.SECOND_API,
      payload

    }
  } catch (err) {
    console.log(`err`, err);
  }
};

const thirdApiCall = async() => {
  try {
    await axiosInstance.patch(
      process.env.THIRD_API,
      payload
    );
  } catch (err) {
    console.log('err', err);
  }
};

firstApiCall();
secondApiCall();
thirdApiCall();

You're calling the functions synchronously when you need to do it asynchronously:当您需要异步调用函数时,您正在同步调用这些函数:

 async function performTasks() {
    await firstApiCall();
    await secondApiCall();
    await thirdApiCall();
  }
  performTasks();

You can use the ES6 Promise implementation approacg.您可以使用 ES6 Promise 实现 approacg。 Therefore you should take a look to this ressource : [Promise][1]因此你应该看看这个资源:[Promise][1]

With the promise approach you can react at each step / each API call.使用承诺方法,您可以在每个步骤/每个 API 调用中做出反应。 [1]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then [1]: https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then

async await functions work only in their local scope. async await函数仅在其本地范围内工作。 For example:例如:

const myFunc = async() => {
  try{
    //...
    await foo();
    //All the code below will be blocked till 
    //the promise return by foo function is resolved
  }
  catch{
    //...
  }
}

const main = () => {
  myFunc();
  otherFunc();
  //Other function calls
  //Regardless of using async await in myFunc, 
  //the code bellow myFunc will be executed as
  //async await will work only in myFunc block scope
}

main()

What you can do is, use async await inside the main function, so that the functions would be called in an order您可以做的是,在 main 函数中使用async await ,以便按顺序调用这些函数

const main = async () => {
  await myFunc();
  await otherFunc();
}

暂无
暂无

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

相关问题 axios 调用从第一个 api 获取响应并传递到第二个 api - axios call getting response from first api and passing into second api 如何使用 React 在 Apollo 客户端中从第一个 api 调用收到的数据进行第二个 api 调用? - How to make a second api call with data received from the first api call in Apollo client with React? React,根据第一个 api 调用设置的 state 进行第二个 API 调用 - React, make a second API call based on a state set by a first api call 使用 React Hooks 在第二次渲染时调用 API - Make an API call on second render using React Hooks 如何使用React Table和Axios进行API调用并在React JS中显示它? - How to make the API call and display it in React JS using React Table and Axios? 在第二个 axios api 调用上获取错误 400 错误请求 - GET error 400 bad request on second axios api call 如何根据从第一次获得的值进行第二次 API 调用。 使用 React 和 useEffect 钩子 - How to Make A second API call based on the value gotten from the first. with React and useEffect hooks 在 React 中使用 useEffect API 调用时出错 - Error when using useEffect API call in React 如何使用反应钩子从第一个 api 调用结果中获取第二个 api 调用需要特定数据的序列 api 调用? - How to fetch sequence api calls where second api call need particular data from first api call result using react hooks? 使用 Axios 和 React 从另一个 API 调用 API - Call an API from another API using Axios and React
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM