This is an incredibly simple example taken from the Axios documentation and various blog posts:
All I'm trying to do is return data from an async function and call it elsewhere:
In file: axios.js:
const axios = require("axios");
async function getJson() {
const url = "https://jsonplaceholder.typicode.com/posts/1";
const response = await axios.get(url);
const data = response.data;
return data;
}
console.log(getJson());
Then I run node axios.js
But instead of logging out the actual Json data from the api as expected, its logging the Promise with:
Promise { <pending> }
This very simply example is taken from this post: https://scotch.io/tutorials/asynchronous-javascript-using-async-await (Above the error handling section).
Is there something fundamental that I'm misunderstanding here? Sorry this is incredibly frustrating, I have read several blog posts and stack overflow articles, and nothing explains this or provides an answer.
For now I'm doing this one file but later the idea is simply to import and call this function in another file and get the data returned by the function.