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

race continues to subscribe when won via complete or error #4808

Closed
cartant opened this issue May 25, 2019 · 0 comments
Closed

race continues to subscribe when won via complete or error #4808

cartant opened this issue May 25, 2019 · 0 comments
Labels
bug Confirmed bug

Comments

@cartant
Copy link
Collaborator

cartant commented May 25, 2019

Bug Report

This is a re-write of the bug found in #4806.

Current Behavior

When a race is won by a source observable that completes or errors synchronously, the implementation continues to subscribe to subsequent source observables.

This does not happen if the source observable that wins the race does so by emitting a value notification.

Reproduction

This code:

import { defer, EMPTY, of, race, throwError } from "rxjs";
import { delay } from "rxjs/operators";

race(
  defer(() => {
    console.log("subscribed to 1");
    return of(1).pipe(delay(100));
  }),
  defer(() => {
    console.log("subscribed to EMPTY");
    return EMPTY;
  }),
  defer(() => {
    console.log("subscribed to 3");
    return of(3).pipe(delay(300));
  })
).subscribe({
  complete: () => console.log("won"),
  error: () => console.log("won with error")
});

will output:

subscribed to 1
subscribed to EMPTY
won
subscribed to 3

and this code:

race(
  defer(() => {
    console.log("subscribed to 1");
    return of(1).pipe(delay(100));
  }),
  defer(() => {
    console.log("subscribed to error");
    return throwError(new Error("kaboom"));
  }),
  defer(() => {
    console.log("subscribed to 3");
    return of(3).pipe(delay(300));
  })
).subscribe({
  complete: () => console.log("won"),
  error: () => console.log("won with error")
});

will output:

subscribed to 1
subscribed to error
won with error
subscribed to 3

Expected behavior

For races won via completion or error, the behaviour should be like this code and subscriptions to subsequent source observables should not be made:

race(
  defer(() => {
    console.log("subscribed to 1");
    return of(1).pipe(delay(100));
  }),
  defer(() => {
    console.log("subscribed to 2");
    return of(2);
  }),
  defer(() => {
    console.log("subscribed to 3");
    return of(3).pipe(delay(300));
  })
).subscribe({
  complete: () => console.log("won"),
  error: () => console.log("won with error")
});

will output:

subscribed to 1
subscribed to 2
won

Environment

  • Runtime: Node
  • RxJS version: 6.5.2
@cartant cartant added the bug Confirmed bug label May 25, 2019
cartant added a commit to cartant/rxjs that referenced this issue May 25, 2019
@benlesh benlesh closed this as completed in f31c3df Jun 4, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Jul 5, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Confirmed bug
Projects
None yet
Development

No branches or pull requests

1 participant