0

Hi I am new to react and at the moment I want to make simple GET requests to an api but all I get is a Promise <pending> when I try to console.log it out

I have the following function

let user = axios.get('/api/v1/users/id/' + userid)
  .then(response => response.data)
  .then( data => { console.log( data ); return data });

console.warn(user);

and it should return a user object and store it in a variable so that I can use it later but it does not work

What have I done wrong here and is there a better way to do this.

Thanks in advance

nad34
  • 343
  • 4
  • 13
  • See https://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call for how to return values from async calls. Also, consider using `async/await`, as it will make things much easier to understand. – Alex Wayne Jul 12 '21 at 16:30
  • Welcome to asynchronous programming in JavaScript! You need to `await` your response, such as `let user = await axois.get(...)` – h0r53 Jul 12 '21 at 16:31

0 Answers0