Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return values from async functions using async-await from function? [duplicate]

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> } 
like image 918
King Rayhan Avatar asked Apr 20 '18 09:04

King Rayhan


People also ask

How can async await return values from async functions?

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.

Can you return from an async function?

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.

Can one async function have multiple awaits?

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).

Can you await a function that returns a promise?

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.

Should you use “return await” in an async function’s return value?

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?

Why do async functions always return a promise?

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.

Is it possible to use async/await with Axios?

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:

How to use await with getdata () function?

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 .)


1 Answers

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()) })() 
like image 164
The Reason Avatar answered Sep 24 '22 06:09

The Reason