0

i want to make axios request from other axios response. and combine the console.log

i have already try

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

json response website 1

{
"data": {
    "trx_id": "T221129CEVD013700",
    "ref_id": "YEPE-780HM434",
    "destination": "101216575|2522",
    "product": "344",
    "status": "Sukses",
    "sn": "мυн. яιfqу29. RefId : S221129031615475TPZI",
},
"status": 1
}

json respon website 2

{
"data": {
    "res": 75000,
    "pbl": 80000
        }
}

so i want to combine like this

status : ${data.data.status}
serial : ${data.data.sn}
product : ${data.data.product}
price : ${data.data.pbl}

when i try it separate each other

Phil
  • 157,677
  • 23
  • 242
  • 245
  • Does this answer your question? [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Phil Nov 30 '22 at 02:37
  • the problem now is how to get thoose respon in one reply – Yandra Prawinata Nov 30 '22 at 08:13

1 Answers1

1

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
}