When dealing with async functions, I know that they always return a Promise
. And to retrieve that value, I have to use .then
or await
.
This is a function returning a Promise
:
const getPeoplePromise = () => {
return fetch('https://swapi.co/api/people')
.then(res => res.json())
.then(data => {
return {
count: data.count,
results: data.results
}
});
};
The only way I know how to retrieve that date is like this:
getPeoplePromise().then(data => {
console.log(`Count data: ${data.count}`);
})
};
But I want something like this (which should be possible, I just fail to nail the syntax I guess):
const i_want_this = getPeoplePromise().then().count
;
which returns undefined
Yes, I know there are several threads/questions on this topic already, but after searching for 30 minutes and going nuts, they all use the value in another function, which is fine and works for me too, I just cannot store the value right away. So this is different.