0

how can I set up value from axios.get method outside? I use TypeScript and I have not found any solution that works.

import axios, { AxiosInstance } from 'axios';

async function data() {
    return await axios.get('https://myUrl/api/v4/3131', {
    headers: {
      'content-type': 'application/json',
      'Authorization': 'Basic auth'
    }
  }).then(response => { return response.data})
  .catch(error => {
    console.log(error);
    return Promise.reject(error);
  });
} 

var data = data();
console.log(response)
  • 1
    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) – jonrsharpe Jan 31 '21 at 18:37

1 Answers1

1

I believe this function returns a promise as is, so you'll have to add .then() and .catch.

data()
.then(data => {
  console.log(data)
})
.catch(err => {
  // Do whatever you want
  alert(err)
})
Levi Harrison
  • 493
  • 3
  • 10