0
const getNewUser = async () => {
    const data = await api.get("/").then((data) => data.data.results[0]);
    console.log(data);
    return data;
  };

  console.log(getNewUser());

In the code above when I console.log(data) in the function I get the correct body as a JSON format however the console.log(getNewUser()) shows a promise as opposed to a JSON object.

api is an axios instance

  • *"even when await is used"* - Except that it isn't... `console.log(getNewUser());` – David Aug 19 '22 at 14:45
  • it will always be promise. Any function implemented on async will always return promise. Maybe what you wanted was `getNewUser().then(console.log)` – Rajesh Paudel Aug 19 '22 at 14:45
  • `getNewUser()` itself returns a promise of a user. This issue has nothing to do with Axios. You created a promise and aren't awaiting it. You should do `console.log(await getNewUser())` instead (this will work on the top level if you're using ESModules. Otherwise, use `getNewUser().then(data => console.log(data))`. – mstephen19 Aug 19 '22 at 14:46
  • @mstephen19 thank you the ```.then``` seemed to fix the issue – AhmedGhassani Aug 19 '22 at 14:52

0 Answers0