Nyahahahaha
Nyahahahaha

Reputation: 111

.asObservable dont want to work with Observable.forkJoin

I have service:

export class ConfigService {
  private _config: BehaviorSubject<object> = new BehaviorSubject(null);
  public config: Observable<object> = this._config.asObservable();

  constructor(private api: APIService) {
    this.loadConfigs();
  }

  loadConfigs() {
   this.api.get('/configs').subscribe( res => this._config.next(res) );
  }
}

Trying to call this from component:

...
Observable.forkJoin([someService.config])
  .subscribe( res => console.log(res) ) //not working 

someService.config.subscribe( res => console.log(res) ) // working
...

How can i use Observable.forkJoin with Observable variable config?

I need to store configs in service and wait unlit them and others request not finished to stop loader.

Upvotes: 5

Views: 1457

Answers (1)

martin
martin

Reputation: 96891

Since you're using BehaviorSubject you should know that you can call next() and complete() manually.

The forkJoin() operator emits only when all of its source Observables have emitted at least one values and they all completed. Since you're using a Subject and the asObservable method the source Observable never completes and thus the forkJoin operator never emits anything.

Btw, it doesn't make much sense to use forkJoin with just one source Observable. Also maybe have a look at zip() or combineLatest() operators that are similar and maybe it's what you need.

Two very similar question:

Upvotes: 7

Related Questions