Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

forkJoin doesn't emit any value for empty array #2816

Closed
klinki opened this issue Aug 28, 2017 · 8 comments
Closed

forkJoin doesn't emit any value for empty array #2816

klinki opened this issue Aug 28, 2017 · 8 comments

Comments

@klinki
Copy link

klinki commented Aug 28, 2017

RxJS version:
5.3.0

Code to reproduce:

Rx.Observable.forkJoin([]).subscribe(data => console.log(data));

Expected behavior:
Code should return empty array.

Actual behavior:
Subscribe is never called.

Additional information:
I think emitting empty array is better, then not emitting at all. User code using forkJoin could handle those situations (for example it might show some message to user).

Also if valid input is array of Observables and output is array of resolved values, then, it should handle situations with empty input array. And I believe empty output array is perfectly valid solution for that.

@kwonoj
Copy link
Member

kwonoj commented Aug 28, 2017

Closing as dupe of #2188 (comment). Forkjoin is intentionally designed to be used for cases where all observable should emit values. Current behavior is expected. When there is any source doesn't emit, forkjoin 'completes' immediately - that's operator's indication.

@kwonoj kwonoj closed this as completed Aug 28, 2017
@klinki
Copy link
Author

klinki commented Aug 28, 2017

I don't see how it is dupe of that comment. Also for empty array, you don't have any Observables at all.

When there is any source doesn't emit, forkjoin 'completes' immediately - that's operator's indication.

I'm sorry, I don't understand this.

@kwonoj
Copy link
Member

kwonoj commented Aug 28, 2017

It is dupe. Forkjoin doesn't consider any other case except all sources emits value, so empty array is also short-curcuit to completion immediately.

In your snippet, you've supplied observer::next only (subscribe(data => console.log(data))) and saying subscribe is never called - but subscribe is actually called in there.

Rx.Observable.forkJoin([]).subscribe(data => console.log(data), null, () => console.log('completed'));

//`completed`

when forkJoin short curcuits, it gives observer::complete immediately without giving signal to observer::next.

@klinki
Copy link
Author

klinki commented Aug 28, 2017

I see. Thanks for explanation.

I'm not sure if it is the right way, but I understand your reasoning. I will create custom wrapper for it.
(I need it to call observer::next even for empty array, because I don't control what is on input - there could be array of Observables, but in certain cases, it could be also empty array.)

@Assdi
Copy link

Assdi commented Jan 30, 2018

how you use observer::next @klinki ?

@klinki
Copy link
Author

klinki commented Jan 30, 2018

@Assdi Actally, I do initial array check, to see if it is not an empty array. And if it is, I don't use forkJoin at all.

Example:

if (observablesArray.length === 0) {
    return Observable.of([]);
} else {    
    return Observable.forkJoin(observablesArray);
}

@Assdi
Copy link

Assdi commented Jan 31, 2018

thanks for ur time 👍 @KevinMcIntyre i did it already

@lock
Copy link

lock bot commented Jun 6, 2018

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Jun 6, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants