Options
All
  • Public
  • Public/Protected
  • All
Menu

A Subject is a special type of Observable that allows values to be multicasted to many Observers. Subjects are like EventEmitters.

Every Subject is an Observable and an Observer. You can subscribe to a Subject, and you can call next to feed values as well as error and complete.

Type parameters

  • T

Hierarchy

Implements

Index

Constructors

Properties

closed: boolean
hasError: boolean
deprecated

Internal implementation detail, do not use directly. Will be made internal in v8.

isStopped: boolean
deprecated

Internal implementation detail, do not use directly. Will be made internal in v8.

observers: Observer<T>[]
deprecated

Internal implementation detail, do not use directly. Will be made internal in v8.

operator: undefined | Operator<any, T>
deprecated

Internal implementation detail, do not use directly. Will be made internal in v8.

source: undefined | Observable<any>
deprecated

Internal implementation detail, do not use directly. Will be made internal in v8.

thrownError: any
deprecated

Internal implementation detail, do not use directly. Will be made internal in v8.

create: (...args: any[]) => any

Type declaration

    • (...args: any[]): any
    • Creates a "subject" by basically gluing an observer to an observable.

      nocollapse
      deprecated

      Recommended you do not use. Will be removed at some point in the future. Plans for replacement still under discussion.

      Parameters

      • Rest ...args: any[]

      Returns any

Accessors

  • get observed(): boolean
  • Returns boolean

Methods

  • Creates a new Observable with this Subject as the source. You can do this to create custom Observer-side logic of the Subject and conceal it from code that uses the Observable.

    Returns Observable<T>

    Observable that the Subject casts to

  • complete(): void
  • Returns void

  • error(err: any): void
  • Parameters

    • err: any

    Returns void

  • forEach(next: (value: T) => void): Promise<void>
  • forEach(next: (value: T) => void, promiseCtor: PromiseConstructorLike): Promise<void>
  • Used as a NON-CANCELLABLE means of subscribing to an observable, for use with APIs that expect promises, like async/await. You cannot unsubscribe from this.

    WARNING: Only use this with observables you know will complete. If the source observable does not complete, you will end up with a promise that is hung up, and potentially all of the state of an async function hanging out in memory. To avoid this situation, look into adding something like timeout, {@link take}, {@link takeWhile}, or {@link takeUntil} amongst others.

    Example

    import { interval, take } from 'rxjs';

    const source$ = interval(1000).pipe(take(4));

    async function getTotal() {
    let total = 0;

    await source$.forEach(value => {
    total += value;
    console.log('observable -> ' + value);
    });

    return total;
    }

    getTotal().then(
    total => console.log('Total: ' + total)
    );

    // Expected:
    // 'observable -> 0'
    // 'observable -> 1'
    // 'observable -> 2'
    // 'observable -> 3'
    // 'Total: 6'

    Parameters

    • next: (value: T) => void

      a handler for each value emitted by the observable

        • (value: T): void
        • Parameters

          • value: T

          Returns void

    Returns Promise<void>

    a promise that either resolves on observable completion or rejects with the handled error

  • deprecated

    Passing a Promise constructor will no longer be available in upcoming versions of RxJS. This is because it adds weight to the library, for very little benefit. If you need this functionality, it is recommended that you either polyfill Promise, or you create an adapter to convert the returned native promise to whatever promise implementation you wanted. Will be removed in v8.

    Parameters

    • next: (value: T) => void

      a handler for each value emitted by the observable

        • (value: T): void
        • Parameters

          • value: T

          Returns void

    • promiseCtor: PromiseConstructorLike

      a constructor function used to instantiate the Promise

    Returns Promise<void>

    a promise that either resolves on observable completion or rejects with the handled error

  • deprecated

    Internal implementation detail, do not use directly. Will be made internal in v8.

    Type parameters

    • R

    Parameters

    Returns Observable<R>

  • next(value: T): void
  • Parameters

    • value: T

    Returns void

  • subscribe(observer?: Partial<Observer<T>>): Subscription
  • subscribe(next: (value: T) => void): Subscription
  • subscribe(next?: null | ((value: T) => void), error?: null | ((error: any) => void), complete?: null | (() => void)): Subscription
  • deprecated

    Instead of passing separate callback arguments, use an observer argument. Signatures taking separate callback arguments will be removed in v8. Details: https://rxjs.dev/deprecations/subscribe-arguments

    Parameters

    Returns Subscription

  • deprecated

    Instead of passing separate callback arguments, use an observer argument. Signatures taking separate callback arguments will be removed in v8. Details: https://rxjs.dev/deprecations/subscribe-arguments

    Parameters

    • next: (value: T) => void
        • (value: T): void
        • Parameters

          • value: T

          Returns void

    Returns Subscription

  • deprecated

    Instead of passing separate callback arguments, use an observer argument. Signatures taking separate callback arguments will be removed in v8. Details: https://rxjs.dev/deprecations/subscribe-arguments

    Parameters

    • Optional next: null | ((value: T) => void)
    • Optional error: null | ((error: any) => void)
    • Optional complete: null | (() => void)

    Returns Subscription

  • toPromise(): Promise<undefined | T>
  • toPromise(PromiseCtor: PromiseConstructor): Promise<undefined | T>
  • toPromise(PromiseCtor: PromiseConstructorLike): Promise<undefined | T>
  • deprecated

    Replaced with {@link firstValueFrom} and {@link lastValueFrom}. Will be removed in v8. Details: https://rxjs.dev/deprecations/to-promise

    Returns Promise<undefined | T>

  • deprecated

    Replaced with {@link firstValueFrom} and {@link lastValueFrom}. Will be removed in v8. Details: https://rxjs.dev/deprecations/to-promise

    Parameters

    • PromiseCtor: PromiseConstructor

    Returns Promise<undefined | T>

  • deprecated

    Replaced with {@link firstValueFrom} and {@link lastValueFrom}. Will be removed in v8. Details: https://rxjs.dev/deprecations/to-promise

    Parameters

    • PromiseCtor: PromiseConstructorLike

    Returns Promise<undefined | T>

  • unsubscribe(): void