Home > other >  axios response data to make other axios request
axios response data to make other axios request

Time:11-29

i want to make axios request from other axios response. i have already try

const data1 = axios.get(`https://website.com/json1respon`).then(({data}) => {
let resp = `product : ${data.data.product}`})
const data2 = axios.get(`https://website2.com/${data.data.product}`).then(({data}) => {
let respt = `price : ${data.data.price}`
reply(resp, respt)
console.log(data)
});

i'm confusing to join it

so i want axios first response to complete url in second request

CodePudding user response:

Just execute the second .get in the then block:

const data1 = axios.get(`https://website.com/json1respon`).then(({ data }) => {
  const data2 = axios
    .get(`https://website2.com/${data.data.product}`)
    .then(({ data }) => {
      let respt = `price : ${data.data.price}`;
      reply(resp, respt);
      console.log(data);
    });
    // Do something with data2...
}).catch(err => console.log(err));

CodePudding user response:

You may use await and async features for better readability and await callback hell for the future.

try {
    const data1 = await axios.get(`https://website.com/json1respon`);
    const data2 = await axios.get(`https://website2.com/${data1.data.product}`);
    let respt = data2.data.price;
} catch (e) {
  // handle error
}
  • Related