0

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 ?

Jazib
  • 1,343
  • 13
  • 27
Rk R Bairi
  • 1,289
  • 7
  • 15
  • 39

1 Answers1

2

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.

Jazib
  • 1,343
  • 13
  • 27