What is the way to return result from the service directly, without returning Observable
and then using then
clause.
I searched for many things, such as using pipe
,of
,take
,toPromise
,map
,async-await
but non of them return result on service call. Also, some of the clauses are not available on current version of rxjs. Can you please help?
Note I have condition here where if api has not worked, I get it from local.
@Injectable()
export class SomeService {
GetDataEitherFromApiOrFromLocalStotage():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 returns null, with async-await
also)
@Component()
export class SomeComponent implements OnInit {
constructor(private service:SomeService) {}
ngOnInit() {
let data = this.service.GetDataEitherFromApiOrFromLocalStotage();
}
}
UPDATE
The reason I'am trying to do this is because I want to put all the logic inside the Service, and not in Component.