Should race have a special case for synchronous observables?
See original GitHub issuePotential Bug Report
Current Behavior
If a race observable is constructed with at least one synchronous observable, the order of events is confusing.
- (Optionally) Subscribe to asynchronous observables that come before the first synchronous observable.
- Subscribe to the first synchronous observable.
- Copy the output from the synchronous observable.
- Subscribe and immediately unsubscribe from all other observables.
Step 4 could be ommitted in my opinion. If this is intended behavior, maybe put a note about it in the documentation?
Reproduction https://stackblitz.com/edit/rxjs-ec1mwd
import { race, EMPTY } from 'rxjs';
import { create } from 'rxjs-spy';
import { tag } from 'rxjs-spy/operators';
import { delay, map } from 'rxjs/operators';
create().log(/.*/);
const source = race(
EMPTY.pipe(delay(100), tag("e0")),
EMPTY.pipe(tag("e1")),
EMPTY.pipe(delay(100), tag("e2")),
EMPTY.pipe(delay(100), tag("e3")),
).pipe(
tag("output"),
)
source.subscribe();
Tag = output; notification = subscribe; matching /.*/
== Step 1
Tag = e0; notification = subscribe; matching /.*/
== Step 2
Tag = e1; notification = subscribe; matching /.*/
Tag = e1; notification = complete; matching /.*/
== Step 3
Tag = output; notification = complete; matching /.*/
Tag = output; notification = unsubscribe; matching /.*/
Tag = e0; notification = unsubscribe; matching /.*/
Tag = e1; notification = unsubscribe; matching /.*/
== Step 4
Tag = e2; notification = subscribe; matching /.*/
Tag = e2; notification = unsubscribe; matching /.*/
Tag = e3; notification = subscribe; matching /.*/
Tag = e3; notification = unsubscribe; matching /.*/
Issue Analytics
- State:
- Created 4 years ago
- Comments:8
Top Results From Across the Web
What happens to other observables when rxjs.race() has been ...
race: race() will no longer subscribe to subsequent observables if a provided source synchronously errors or completes.
Read more >Working with observables in Angular: what we learned at Whoz
Whatever you need to do, going synchronous by subscribing to an observable is never the answer. There is always an operator ready to...
Read more >Observables Or Promises - DEV Community
Observables were introduced to JavaScript due to the lack of native support for multiple streams of asynchronous data/event in JavaScript.
Read more >Chapter 1. Reactive Programming with RxJava - O'Reilly
Generally, an Observable is going to be asynchronous, but it doesn't need to be. An Observable can be synchronous, and in fact defaults...
Read more >Callbacks vs Promises vs RxJS vs async/await - Academind
The same would be the case for functions where you reach out to a web ... you may have a look at Promise.all()...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Absolutely fine with me. Close it once you created the new one.
Closed in favour of #4808