I want to do a api call and dont want to wait for its response. Like below.
{
// Some Work
this.authService.logUserDetails(); // this will be api call for which i don't want to wait.
// continue with some work..
}
I'm not sure why you would want such a thing but in order to actually do the API call you need to subscribe to it.
{
// Some Work
this.authService.logUserDetails().subscribe();
foo();
bar();
baz();
}
The code executed inside the subscribe callback will be asynchronous, meaning that the code will be executed when the response will be available.
The code below the subscribe instead will be executed while the response is fetched.
What exactly do you mean? if that is an api call, you will have to subscribe to it, subscribe is already asynchronous. Its not like you dont want to wait for it. You can not wait for it, without somehow implementing promise like behaviour.
You can just go
someFunction() {
this.authService.logUserDetails().subscribe({
next: (response) => {
// do smth with response
},
error: (error) => {
// treat error
}
})
//logic that is written here will be run before logic that is written inside subscribe
}
Have in mind that api call will not actually be done before you subscribe to it