I wanted to force this function to be asynchronous, that's why I've given it to him, async
but await
he, who was trying to put it in the return , won't let me.
Where should I put the await
?
public async loginU(usuario:string, password:string){
return this._http
.get<Usuario>(this.URI+"/existU/"+usuario+"/"+password)
.pipe(
map(usuario=>{
return usuario;
})
)
}
It doesn't really go anywhere.
async
andawait
they are for working with promises and the methods ofHttpClient
what they return are Observables .Observables are much more powerful than promises since they allow, among other things, to cancel the execution of the http request, so I recommend you keep them instead of changing to promises.
In your code it would be:
And elsewhere in your code:
The part
subscribe()
is what triggers the http call.It could also be:
If you want to call it directly from that method.
By the way, write:
It won't do anything since it
map
's a transform operation and you're returning the value of the parameter exactly as you received it.I imagine that what you were actually trying to do was assign the value to a variable of your component and for that you should not use it
map
buttap
it is the one that allows you to execute secondary effects in the chain of operators of the observable. Something like this:After the request is executed, the variable
usuario
that is available in the templatepublic
will have the value you need.Is that code Angular?
From what I see, I understand that _http is an object of type HttpClient.
If so then the loginU function is already asynchronous and wherever you are calling it you have to do it with a subscribe, you don't need to use async/await in this case.
Async/await is used with promises, but in this case you're doing a subscription.