Why axios, promises can return, and the result of the promis — no?

Why is the first use case works and the second option is not.
Why not just get the result of promis?
5dee9ff46aaec208791074.png
5dee9ffece2a2360582578.png
April 3rd 20 at 17:13
3 answers
April 3rd 20 at 17:15
Because asynchrony.
async getUser(userId) {
return await instance.get('...')
}


Are kindly requested not to upload the screenshots and use the markup code embedded in the platform.
Because here and there I do asynchronously, but it works in the first case. And why when the query is executed asynchronously, leaving the event loop I can't give the result, because he's done?! - mattie.White commented on April 3rd 20 at 17:18
@mattie.White, await waiting for resolve of promis, while just thrown out retornam then will return a promise, the status of "pending", if my memory serves me. - ada commented on April 3rd 20 at 17:21
April 3rd 20 at 17:17
Why is the first use case works and the second option is not.

because they have different output, regardless of promises.
In the first case will return a promise which will be the result of the query.
in the second case, return a promise which will be the data from the query result.

for example, in the first case, the data is `{data: {x:1}}` and the second `{x:1}`

it is obvious that different data lead to different results.

Promis or promis - minor matters, in terms of promises, both versions are identical.
The results of the return is clear. Here is my componentdidmount: 
For the first case:

 componentDidMount() {
serverAPI.getUser(2).then(e=>{this.props.setFriends(e.data)});
}
Why should I process the result of promis in compDidMount < and not to do this processing in serverAPI , in getUser method and return the result. Here it is: 

export const serverAPI = {
 getMyUser(userID) {
 return instance.get (`/profile / ${userID}`).then(e=>e.data)
},
};
and therefore compDidMount have to throw this result is in store 
Here it is:

 componentDidMount() {
this.props.setFriends(serverAPI.getUser(2))
}
- mattie.White commented on April 3rd 20 at 17:20
Why should I process the result of promis in compDidMount


Who told you that you should?
Can and serverApi to do.

you see the difference between
serverAPI.getUser(2).then(e=>{this.props.setFriends(e.data)});

and
this.props.setFriends(serverAPI.getUser(2))


If not - then I can sure you fix the code, but you will come to the next component with the same question.
The first code is waiting for promis, gets data from it and sends this data to setFriends
The second code expects nothing, takes the promis and its otpravlyaet setFriends (Shocker, they wanted data and you promis slipped)

As you already said - understand how promises work. From the point of view of logic. But then so will rearrange the methods and parenthesis not realizing that is not so. - jake_Robel commented on April 3rd 20 at 17:23
@jake_Robel, Spasibo big, understood - mattie.White commented on April 3rd 20 at 17:26
April 3rd 20 at 17:19
You know how promises work, right?

In both cases, when you call getUser() will return a promise, but wasresolved it with different parameters
What is the "result of promise"?)

Promis returned in the getUser, makes a request which is loaded conditionally, 10 seconds. But the method getUser is obliged to return something immediately after your call. It returns a promise, the status of pending until the promise is resolved/rejected . If the promise is resolved, then method will be executed and you will be able to return from promis some xtone value
Became a little clearer?

Find more questions by tags JavaScriptReact