-2

That my Code trying to returning response.data.

const getRobots = () => {
  axios.get("https://jsonplaceholder.typicode.com/users").then((response) => {
    return response.data;
  });
};

let robotsValue = getRobots();
console.log(robotsValue);

Pending

  • Please see [How do I return the response from an aynchronous call](https://stackoverflow.com/q/14220321/438992), which this duplicates. You return the value of `axios.get`. You want to `await` it (meaning an async function) and/or wrap your head around async programming in general. – Dave Newton Sep 22 '20 at 14:06
  • 1
    Your last question was closed as a duplicate https://stackoverflow.com/q/64011355/691711, don't just repost it. – zero298 Sep 22 '20 at 14:06

1 Answers1

-1

You need to use the then after you call the function, not inside it. I changed it to fetch so i could show you in a snippet.

const getRobotsThen = () => {
  return fetch("https://jsonplaceholder.typicode.com/users");
};

getRobotsThen()
  .then(res => res.json())
  .then(data => console.log(data));

Another option is to use the async await combo

const getRobotsAsync = async () => {
  const res = await fetch("https://jsonplaceholder.typicode.com/users");
  return await res.json();
}

// await only works in an async function, thats why there is an IIFE
(async () => {
  let robotsValueAsync = await getRobotsAsync();
  console.log(robotsValueAsync);
})()
Reyno
  • 6,119
  • 18
  • 27