V
V
Vann Damm2019-12-09 22:27:30
JavaScript
Vann Damm, 2019-12-09 22:27:30

Why can axios, promises be returned, but not the result of a promise?

Why the first use case works and the second one doesn't.
Why can't you get the result of a promise right away?
5dee9ff46aaec208791074.png
5dee9ffece2a2360582578.png

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
RMate, 2019-12-09
@RMate

Because asynchrony.

async getUser(userId) {
return await instance.get('...')
}

The most convincing request is not to insert screenshots, but to use the code markup tools built into the platform.

R
Robur, 2019-12-10
@Robur

Why the first use case works and the second one doesn't.

because they have different output data, regardless of promises.
In the first case, a promise will be returned in which the result of the query will be.
in the second case, a promise will be returned in which there will be a data field from the query result.
for example, in the first case the data will contain `{data: {x:1}}` and in the second case `{x:1}`
it is obvious that different data will lead to different results.
A promise there or not a promise is the tenth thing, in terms of promises, both options are identical.

E
Egor Zhivagin, 2019-12-10
@Krasnodar_etc

You understand how promises work, right?
In both cases, when you call getUser() , a promise will be returned, but it will be resolved with different
parameters
. But the getUser method must return something as soon as it is called. It returns a pending promise until the promise is resolved/rejected . If the promise is resolved, the then method will be executed and you will be able to return some custom value from the promise. Has it
become a little clearer?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question