How do I refactor this fetch request? I have this code that fetches data from a 3rd party api and sends it to my backend to be processed. The 3rd party api needs to target the user's localhost address so it has to come from the front end. I have the code below which works perfectly fine but it looks messy, I'm sure there is a better way to write it. Any suggestions?
I am using fetch for the original request because if I use axios to the 3rd party api I get a cors error.
  getLogs = () =&gt; {
    const { user } = this.props.auth;
    fetch(`http://127.0.0.1:60000/onexagent/api/registerclient?name=${user.id}`)
      .then((res) =&gt; res.text())
      .then((result) =&gt; {
        const regxml = new XMLParser().parseFromString(result);
        if (regxml.attributes.ResponseCode === &quot;0&quot;) {
          axios.post(`/api/avaya/register/${user.id}`, regxml);
        }
        console.log(regxml);
      });

    fetch(`/api/avaya/getid/${user.id}`)
      .then((res) =&gt; res.text())
      .then((result) =&gt; {
        fetch(
          `http://127.0.0.1:60000/onexagent/api/nextnotification?clientid=${result}`
        )
          .then((res) =&gt; res.text())
          .then((result) =&gt; {
            const xml = new XMLParser().parseFromString(result);
            axios.post(`/api/avaya/getlogs/${user.id}`, xml);
          });
      });
  };

  timer = (time) =&gt; {
    const date = new Date(time);
    return `${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`;
  };

  componentDidMount() {
    this.getLogs();
    this.interval = setInterval(this.getLogs, 5000);
  }

  componentWillUnmount() {
    clearInterval(this.interval);
  }
Let me share my async await solution for that:

Because Your fetch function is pretty similar till the res.text(), I would create an own function (fetchData ) for that, what is returning a Promise, what is resolving a res.text()
Now you can assign an async to your getLogsfunction
Lets reorganise your code above
Error handling is missing here, but take care about it 

:
  fetchData = (url) =&gt; {
    return new Promise((res, rej) =&gt; {
      fetch(url)
        .then(r =&gt; r.text())
        .then(text =&gt; {
          res(text)
        })
        .catch(e =&gt; rej(e));
    });
  }

  getLogs = async () =&gt; {
    const { user } = this.props.auth;
    const regxmlPromise = fetchData(`http://127.0.0.1:60000/onexagent/api/registerclient?name=${user.id}`);
    const secondDataPromise = fetchData(`/api/avaya/getid/${user.id}`);
   
    const resolves = await Promise.all([regxmlPromise, secondDataPromise]);
    
    const [firstResultStepOne, secondResultStepOne] = resolves || [];
    
    const secondResultStepTwo = await fetchData(`http://127.0.0.1:60000/onexagent/api/nextnotification?clientid=${secondResultStepOne}`);

    if (firstResultStepOne) {
      const regxml = new XMLParser().parseFromString(firstResultStepOne);
      if (regxml.attributes.ResponseCode === &quot;0&quot;) {
        axios.post(`/api/avaya/register/${user.id}`, regxml);
      }
      console.log(regxml);
    }
    
    if (secondResultStepTwo) {
      const xml = new XMLParser().parseFromString(secondResultStepTwo);
      axios.post(`/api/avaya/getlogs/${user.id}`, xml);
    }
  };