I am having a SEARCH button in my component and I want to call 2 different APIs when user clicks on this button.
<div className="ShipCard">
<div className="grid2">
<h1>Enter SO Gateway ID </h1>
<input type="text" name="gatewayID" onChange={changeData}/>
</div>
<button className="ShipSearch" onClick={onSearch}>Search</button>
</div>
Assume 2 different APIs are A and B.
I want to call B after A.
How can this be achieved ?
in main component
import {A,B} from './service';
const onSearch = () =>{
A().then(response)=>{}
B().then(response)=>{}
}
In Service component
const A =async() =>{
return await axios
.get("https://xyz")
}
const B =async() =>{
return await axios
.get("https://abc")
}
export {A,B};
3
Answers
Use
await
.use can use the below async function. Once the first request resolve it will move to the next line due to async await.
You can combine both
axios.get
into one call. Such as