cerealex
cerealex

Reputation: 1691

Wait for two observables finish with zip

This is the method code. I'm trying to wait for two observables to complete using zip, but nothing logs, not even the http calls in fooProvider are called.

let obsArray: Observable<any>[] = [];

this.idArray.forEach(id => {
  obsArray.push(this.fooProvider.bar(id, 1));
});

Observable.zip(obsArray)
  .subscribe(res => console.log(res));

Upvotes: 3

Views: 443

Answers (1)

t.888
t.888

Reputation: 3902

Observable.zip takes arguments (a, b, ..., n). I wasn't able to make it work with an array directly, but it should work with apply:

const zip = Observable.zip
zip.apply(null, obsArray).subscribe(...)

apply will convert the given array into a function call with the array elements passed as arguments.

EDIT: As per @martin's comment, you can also use the spread operator if your environment supports it. zip(...obsArray).subscribe(...)

Upvotes: 1

Related Questions