Reputation: 11864
My console.log is never call (my 2 requests is called and OK).
const obs1 = this.awsService.getUsers();
const obs2 = this.apiService.get('admin/user');
return forkJoin(obs1, obs2).subscribe((res) => {
console.log('res2', res);
});
I try unit call (console.log A and B is ok):
const obs1 = this.awsService.getUsers();
obs1.subscribe((res) => {
console.log('A' + res);
});
const obs2 = this.apiService.get('admin/user');
obs2.subscribe((res) => {
console.log('B' + res);
});
EDIT
My code return a observable of User[] but KO un a forkJoin:
getUsers(): Observable<User[]> {
return Observable.create(obs => {
const u = new User;
u.username = user.Username;
_users.push(u);
return obs.next(_users);
});
}
This code is OK:
const u1: User = new User;
u1.username = 'foo';
const u2: User = new User;
u2.username = 'foo';
const o2: Observable<User[]> = of([u1, u2]);
const fj = forkJoin(o2);
fj.subscribe(res => {
console.log('r');
});
// r is ok in console
What difference between of
and Observable.create( ... return obs.next(_users);)
?
Upvotes: 0
Views: 1417
Reputation: 29335
the difference between create and of is that of by definition completes after emitting one item. create does not complete unless you specify it.
if you did:
getUsers(): Observable<User[]> {
return Observable.create(obs => {
const u = new User;
u.username = user.Username;
_users.push(u);
obs.next(_users);
obs.complete();
return () => {}; // return value should be clean up function on completion if needed
});
}
then it will work because you complete immediately after emitting. forkJoin expects all streams to complete and will not emit until all streams have, and it will emit only with the latest value in the streams.
If you actually want all emissions, consider combineLatest instead
Upvotes: 1