4
data = [{'info':'success'},{'info':'fail'}];
public getResults(page: Page,data:any[]): Observable<PagedData<CorporateEmployee>> {
    return Observable.of(data).map(data => this.getPagedData(page,data));
}

In the above code what is the purpose of the Observable.of operator?

Ragavan Randy
  • 171
  • 1
  • 1
  • 12
  • The purpose is what [the documentation](https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/core/operators/of.md) says it is. Also, this has nothing to do with Angular, other than the fact that rxjs is commonly used in Angular. Nor does it have anything to do with TypeScript. –  Sep 20 '17 at 05:26
  • You could also take a look at http://reactivex.io/rxjs/class/es6/Observable.js~Observable.html#static-method-of. –  Sep 20 '17 at 05:34
  • `of` is not an operator, by the way. The term "operator" refers to things that are applied to observable instances, such as `map`. –  Sep 20 '17 at 09:35
  • Oh! Ok, @torazaburo. I am new Angular and RXJS. I consider the 'of' is the operator of Observable before your information. Now i clear. Thanks for your explanation. – Ragavan Randy Sep 20 '17 at 10:03
  • Possible duplicate of [Why we should use RxJs Of() function?](https://stackoverflow.com/questions/47889210/why-we-should-use-rxjs-of-function) – Aref Zamani Sep 25 '18 at 14:14

1 Answers1

6

It wraps the array object into the Observable stream for later Observable processing. Your function returns an Observable, the map function is the Observable function (also array has this), and if you want to work with Observable stream, you need to convert your array into the Observable stream and use its functions.

Suren Srapyan
  • 66,568
  • 14
  • 114
  • 112