title: axios in javascript function to return data
body: <p>I have 2 files in javascript.</p>
  <p>One:
  main_app.js</p>
  <pre><code>import * as API from &quot;./api.js&quot;;
  
  const pin = API.getPin();
  
  console.log(pin);
  </code></pre>
  <p>other is api.js</p>
  <pre><code>const baseUrl = &quot;https://some.com/api&quot;;
  
  export function getPin() {
    try {
      axios.post(baseUrl, { method: &quot;pin&quot; }).then((res) =&gt; console.log(res));
    } catch {
      console.log(&quot;Fail&quot;);
    }
  }
  </code></pre>
  <p>In the first file I need to save the output of the getPin() but I always get Promise - pending in the console.
  In the axios call and .then method I can console.log the correct data but whatever I try I am not able to return the data.</p>
  <p>I have also tried async and await with no avail.</p>
  <pre><code>export async function getPin() {
    try {
      const res = await axios.post(baseUrl, { method: &quot;pin&quot; }).then(res =&gt; res.data);
  
      return res.data;
    } catch {
      console.log(&quot;Fail&quot;);
    }
  }
  </code></pre>
  <p>I have tried class from es6 but with the same result and then thought I am probably not using the class correctly:</p>
  <pre><code>export class Api {
    static baseUrl = &quot;https://some.com/api&quot;;
    static response = null;
  
    static getPin() {
      const body = { method: &quot;pin&quot; };
  
      axios
        .post(this.baseUrl, body)
        .then((res) =&gt; (this.response = res.data))
        .catch((err) =&gt; console.error(err));
    }
  }
  </code></pre>
  <p>I ve look at many articles and I am sure I am missing something obvious but I am losing my mind :-(
  Any help would be appreciated</p>
  <p>EDIT: Not really a duplicate with suggested question.I have re-did the class and it looks like this</p>
  <pre><code>export default class API {
    static baseUrl = &quot;https://some.com/api&quot;;
    static response = null;
    static pin = null;
  
    static getPin() {
      axios
        .post(this.baseUrl, { method: &quot;pin&quot; })
        .then((res) =&gt; (this.pin = res.data.pin));
  
      return this.pin;
    }
  }
  </code></pre>
  <p>but this will STILL not working as I need.Help please</p>