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> }
To return values from async functions using async-await from function with JavaScript, we return the resolve value of the promise. const getData = async () => { return await axios.
Async functions always return a promise. If the return value of an async function is not explicitly a promise, it will be implicitly wrapped in a promise. Note: Even though the return value of an async function behaves as if it's wrapped in a Promise.resolve , they are not equivalent.
In order to run multiple async/await calls in parallel, all we need to do is add the calls to an array, and then pass that array as an argument to Promise. all() . Promise. all() will wait for all the provided async calls to be resolved before it carries on(see Conclusion for caveat).
The keyword await is used to wait for a Promise. It can only be used inside an async function. This keyword makes JavaScript wait until that promise settles and returns its result.
Since the return value of an async function is always wrapped in Promise.resolve, return await doesn’t actually do anything except add extra time before the overarching Promise resolves or rejects. The only valid exception is if return await is used in a try/catch statement to catch errors from another Promise-based function. So, how to decide?
async functions always return promises. async / await exists to simplify the syntax of working with promises, not to eliminate promises. The code that's using your async function will need to call .then on the promise, or be an async function itself and await the promise.
Since axios returns a promise the async/await can be omitted for the getData function like so: Show activity on this post. your function getData will return a Promise. 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:
your function getData will return a Promise. 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: (I named the function for sake of clarity, but in this scenario, one would rather use an anonymous function call; see TheReason's answer .)
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()) })()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With