193

How can I return the value from an async function? I tried to like this

const axios = require('axios');
async function getData() {
    const data = await axios.get('https://jsonplaceholder.typicode.com/posts');
    return data;
}
console.log(getData());

it returns me this,

Promise { <pending> }
4
  • console.log(await getData()); (if this code is another function, then it need to be marked as async as well) Commented Apr 20, 2018 at 9:22
  • 7
    or treat this as a Promise, and do it the 'old' way : getData().then(x => { console.log(x); } ) Commented Apr 20, 2018 at 9:23
  • 8
    @pac0: That does not return a value, which is what the question is about. Commented Aug 4, 2020 at 11:40
  • In the developer console the following works async function getData() {return await fetch('https://jsonplaceholder.typicode.com/posts');} followed by just await getData(). Commented Jun 5, 2021 at 5:55

4 Answers 4

175

You cant await something outside async scope. To get expected result you should wrap your console.log into async IIFE i.e

async function getData() {
  return await axios.get('https://jsonplaceholder.typicode.com/posts');
}

(async () => {
  console.log(await getData())
})()

Working sample.

More information about async/await

Since axios returns a promise the async/await can be omitted for the getData function like so:

function getData() {
  return axios.get('https://jsonplaceholder.typicode.com/posts');
}

and then do same as we did before

(async () => {
   console.log(await getData())
})()
Sign up to request clarification or add additional context in comments.

5 Comments

(async () => { console.log(await getData()) })() what's going on here?
are you running a annonymous async function. right?
but you can use another 'named' function instead, if you need.
@KingRayhan You're right, an anonymous async function is being invoked in place. Such anonymous function are called Immediately Invoked Function Expressions (IIFEs) in JS.
"To get expected result you should wrap your console.log into async IIFE i.e" is now outdated, we have top level await in most JS environments.
41

your function getData will return a Promise.

So you can either:

  • await the function as well to get the result. However, to be able to use await, you need to be in an async function, so you need to 'wrap' this:

    async function callAsync() {
       var x = await getData();
       console.log(x);
    }
    callAsync();
    

    (I named the function for sake of clarity, but in this scenario, one would rather use an anonymous function call; see TheReason's answer.)

or

  • use the result as a normal Promise, which is what an async function returns.
    You have to use then with a callback:

    getData().then(x => { 
        console.log(x); 
    });
    

Comments

25

The other answers have covered this fine; but I'd like to chip in and say get in the habit of creating and calling a main function rather than run things in the global scope. i.e.

async function main(){
  let result = await getData();
}

main().catch(console.log);

This is pretty clear to anyone reading your code that this is your app entry point

1 Comment

Thanks - this is the best approach, imho, and helped me realize why I couldn't call an async method defined in a clasa at the root of a node module
0

Or I can do implicit return from an anonymous function.

async function getData() {
  return await axios.get('https://jsonplaceholder.typicode.com/posts');
}
(async () => console.log(await getData()) })()
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.18.0/axios.js"></script>

1 Comment

Running the code snippet throws an error.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.