title: Using axios in a javascript function without global variables
body: <p>I'm trying to understand how to use axios (<a href="https://cdnjs.cloudflare.com/ajax/libs/axios/0.17.0/axios.js" rel="nofollow noreferrer">https://cdnjs.cloudflare.com/ajax/libs/axios/0.17.0/axios.js</a>) within a function to talk to my own backend to get some data.</p>
  
  <p>The code below works.</p>
  
  <p>I need to define a global variable outside of the function to catch the result. </p>
  
  <p>If I move the variable <em>myresponse</em> into the function, I can no longer catch the reply.</p>
  
  <p>How can I avoid the global variable?</p>
  
  <pre><code>var myresponse = "xxx"
  
  function getTimeData() {
  
      axios.get('/time')
      .then(function (response) {
  
      console.log(response.data);
          myresponse = response.data;
      })
      .catch(function (error) {
          myresponse = "error";
      });
  
      return myresponse;
  
  }
  
  console.log(getTimeData())
  </code></pre>
  
  <p>For this example I run a local server at "/time" that returns a timestring.</p>