/    Sign up×
Community /Pin to ProfileBookmark

returns [object promise] instead of the data

i fetch some data in getData() and then it gets returned but i cant use the returned data since it becomes an [object promise]. i have tried alot of things but cant get it to work

“`
const test = async (a) => {
const data= await getData(a)
console.log(data) // **logs the correct data**
return data
}
console.log(“asd: “+ test(1)) // **logs [object promise] instead of the data**
“`

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@NachfolgerDec 25.2020 — What are you expecting it to return? It's an async function, you need to use the promise that it returns.

``JavaScript<i>
</i>const test = async (a) =&gt; {
const data = await getData(a);
console.log(data);
return data;
}

test(1).then(d =&gt; {
console.log(d)
});<i>
</i>
`</CODE>

This code:
<CODE lang="JavaScript">
`JavaScript<i>
</i>const test = async (a) =&gt; {
const data = await getData(a);
console.log(data);
return data;
}<i>
</i>
``

Is automatically wrapped in a promise, even if you don't write it.
Copy linkTweet thisAlerts:
@cvemauthorDec 25.2020 — @Nachfolger#1626171

what i want to do is get the String that getData() returns. but for some reason i cant.

instead of getting the string, i get [object Promise]

console.log(getData(1)) // logs [object Promise] but should log the string that getData() returns

this is what getData() returns:
``<i>
</i>if (aaa.data) {
let d = aaa.data.results[0]["key"]
console.log(typeof d) // says its a string
return d
} else {
return "false"
}<i>
</i>
``
Copy linkTweet thisAlerts:
@SempervivumDec 25.2020 — Read the article in this link in order to get familiar with async/await:

https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Async_await
Copy linkTweet thisAlerts:
@cvemauthorDec 26.2020 — Thank you guys.

I dont seem to be able to solve my problem tho.

How do i get the returned value out of the promise scope?

``<i>
</i>axios.get("#").then(res =&gt; {
....
....

let test = ""
// getData(1).then(r =&gt; console.log(r))
getData(1).then(r =&gt; test = r) // doesnt work
})<i>
</i>
``
Copy linkTweet thisAlerts:
@SempervivumDec 26.2020 — Promises are used mainly when requesting data from the server by a get or post request. In this situation one encounters the following problem: After sending the request it will take some time until the response from the server arrives. Therefore the data provided by the request cannot be returned immediately but a promise is returned instead. As visible in the code nachfolger posted you need to use `then</C> in order to get the data instead of the response. The data is available inside <C>then</C> only, therefore you need to do all actions based on the data inside <C>then`:
const test = async (a) =&gt; {
const data = await getData(a);
console.log(data);
return data;
}

test(1).then(data =&gt; {
console.log(data);
// perform all actions that access data here
});


BTW: MDN (link I posted above) says about asyns/await:
> It makes code much simpler and easier to understand

I do not agree with this statement, IMO the fetch API is more clear and easy to understand.
×

Success!

Help @cvem spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.24,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...