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
in order to get the data instead of the response. The data is available inside then
only, therefore you need to do all actions based on the data inside then
:
const test = async (a) => {
const data = await getData(a);
console.log(data);
return data;
}
test(1).then(data => {
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.