Is there a way to directly return a result from the service without returning Observable
and then using then
clause? I've experimented with methods like pipe
, of
, take
, toPromise
, map
, async-await
, but none of them seem to return the result on a service call. Some of these methods aren't even available in the current version of RxJS. Can you provide any assistance?
Note: There is a condition where if the API fails, I need to retrieve data locally.
@Injectable()
export class SomeService {
GetDataEitherFromApiOrFromLocalStorage(): any
{
let result;
this.http.get("https://"+ this.url +"/api/main/apidata").subscribe(
next => {result = next;},
error => {result = this.localData();},
() => {return result;}
)
}
}
When calling the function (which currently returns null, even with async-await
)
@Component()
export class SomeComponent implements OnInit {
constructor(private service: SomeService) {}
ngOnInit() {
let data = this.service.GetDataEitherFromApiOrFromLocalStorage();
}
}
UPDATE:
I'm attempting this because I'd like to keep all the logic within the Service, rather than in the Component.