Hyper
Hyper

Reputation: 337

rxjs call combineLatest in combineLatest

I got a problem with rxjs and with combineLatest method. I am trying to call combineLatest in combineLatest and it doesn't work, although it returns Observable object. Help to resolve the problem, please. console.log is never called

Actually, all the observers in the different files, so I can't move this.search$ to this.services$

this.search$ = store.select('search');

this.services$ = Observable.combineLatest(
    store.select('currentRegions'),
    store.select('services'),
    (regions, services) => {
        // some filter here
        return services;
    }
);

this.autocomplete$ = Observable.combineLatest(
    this.search$,
    this.services$,
    (search, services) => {
        console.log('show me, please');
        return '';
    }
);

Resolved: It doesn't work without any subscriber, so I had to subscribe it

Upvotes: 3

Views: 7339

Answers (2)

STEEL
STEEL

Reputation: 10057

RxJS CombineLatest is kind of AngularJS $q.all

import 'rxjs/add/observable/combineLatest';


Observable
      .combineLatest(this.store.select('currentRegions'), this.store.select('services')
      .do(console.log)
      .subscribe();

Upvotes: 1

martin
martin

Reputation: 96899

The combineLatest() operator emits a value when all its source Observables emit at least one value.

So if the console.log(...) never prints it means that this.search$ or this.services$ never emit anything. In other words this means that one of store.select('currentRegions'), store.select('services') or store.select('search') never emit any value.

Note that you can use startWith() (even with startWith(null)).

This works: https://jsbin.com/gecede/2/edit?js,console

This doesn't work: https://jsbin.com/livafar/2/edit?js,console

Upvotes: 2

Related Questions