Ka Mok
Ka Mok

Reputation: 1988

RXJS, What's the difference between Observable.create and new Observable?

In a template, I set an async pipe for a Observable with.

*ngFor="let checkIn of checkIns | async"

In the component, this.checkIns = this.service.getCheckIns().

In the service, I got:

getCheckIns(): Observable<any> {
  return new Observable((observer) => {
    observer.next(...)
  }
}

I notice if I swap out new Observable() with Observable.create(), there's no difference:

getCheckIns(): Observable<any> {
  return Observable.create((observer) => {
    observer.next(...)
  }
}

Upvotes: 1

Views: 73

Answers (1)

martin
martin

Reputation: 96949

This is correct, these two are synonymous.

See source code: https://github.com/ReactiveX/rxjs/blob/master/src/Observable.ts#L56

The static method Observable.create(...) just calls new Observable(...) under the hood.

Upvotes: 2

Related Questions