angularrxjsangular-httpclientrxjs-observablesrxjs-subscriptions

wrapper observable event on top of http call


I have a function which returns an object. Instead of returning the object synchronously, can I have it returned inside an observable which can be later resolved using callbacks (similar to an http call).

Caller Function:

getResource() {
  this.someService.getData().subscribe((data) => { 
      this.playWithData(data);
    }, (error) {                                   
         console.log(error); 
    });
}

Called Fn

getData() {
  return data; //required to return observable/subscriber object
}

can you let know if this is possible and how to create and return an observable just like how http call would return one ?


Solution

  • Yes, Using Rxjs operators like of

    ...
    import { of } from 'rxjs';
    ...
    
    getData() {
      return of(data);
    }
    

    EDIT: This link has the creational operators of rxjs, find the one meets your need.