1

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.

2 Answers 2

2

you need to await for the async request, for example,

import { getJson } from '../file';
async function printJSON() {
  const jsonData = await getJson();
  console.log(jsonData);
}

because here getJSON is an async function.

Sign up to request clarification or add additional context in comments.

Comments

1

Your function is async which means it returns a Promise, as you've found out but clearly are not expecting.

You need to await the function call. E.g.

console.log(await getJson());

Or, you could also do:

getJson().then(json => {
    console.log(json);
});

Simple Example:


const getAppointment = (appointmentId) => {
    return axios.get("example.com");
};

getAppointment(123).then(response => {

    if (response.status === 200) {
        console.log(response.data); // Do what you want with the JSON
    }

});

8 Comments

You can't do this: console.log(await getJson()); - it says: SyntaxError: missing ) after argument list
Regarding the then example - I thought whole point of async/await is write code that looks synchronous and move away from the then syntax.
@daneasterman I suggest you go away and properly understand the topic, perhaps try watching youtube.com/watch?v=QO4NXhWo_NM. He is very good at explaining the basics. Once you think you understand, then write the code.
Also: getJson().then(json => { console.log(json); }); Prints out everything, not just the data. json.data doesn't help either.
@rhy_stubbs I have been doing a lot of reading and researching on promises to try to understand it before writing the code for my real example. That's also why I have gone back to this simple example, to try to go back to first principles.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.