> For the complete documentation index, see [llms.txt](https://soufatn.gitbook.io/rxjs-book/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://soufatn.gitbook.io/rxjs-book/summary/subjects/async_subject.md).

# Rx.AsyncSubject

Represents the result of an asynchronous operation. The last value before the OnCompleted notification, or the error received through OnError, is sent to all subscribed observers.

This class inherits both from the `Rx.Observable` and `Rx.Observer` classes.

## Usage #\#

The follow example shows caching on the last value produced when followed by an onCompleted notification which makes it available to all subscribers.

```javascript
var subject = new Rx.AsyncSubject();

var i = 0;
var handle = setInterval(function () {
    subject.onNext(i)
    if (++i > 3) {
        subject.onCompleted();
        clearInterval(handle);
    }
}, 500);

var subscription = subject.subscribe(
    function (x) {
        console.log('Next: ' + x.toString());
    },
    function (err) {
        console.log('Error: ' + err);
    },
    function () {
        console.log('Completed');
    });

// => Next: 3
// => Completed
```

### Location

* rx.js

## `AsyncSubject Constructor` #\#

* [`constructor`](/rxjs-book/summary/subjects/async_subject.md#rxasyncsubject)

## `AsyncSubject Instance Methods` #\#

* [`dispose`](/rxjs-book/summary/subjects/async_subject.md#rxasyncsubjectprototypedispose)
* [`hasObservers`](/rxjs-book/summary/subjects/async_subject.md#rxasyncsubjectprototypehasobservers)

## Inherited Classes #\#

* [`Rx.Observable`](https://github.com/Reactive-Extensions/RxJS/blob/master/doc/core/observable.md)
* [`Rx.Observer`](https://github.com/Reactive-Extensions/RxJS/blob/master/doc/core/observer.md)

## *AsyncSubject Constructor* #\#

### `Rx.AsyncSubject()` <a href="#rxasyncsubject" id="rxasyncsubject"></a>

[#](/rxjs-book/summary/subjects/async_subject.md#rxasyncsubject) [Ⓢ](https://github.com/Reactive-Extensions/RxJS/blob/master/src/core/subjects/asyncsubject.js#L33-L42)

Creates a subject that can only receive one value and that value is cached for all future observations.

#### Example

```javascript
var subject = new Rx.AsyncSubject();

subject.onNext(42);
subject.onCompleted();

var subscription = subject.subscribe(
    function (x) {
        console.log('Next: ' + x);
    },
    function (err) {
        console.log('Error: ' + err);
    },
    function () {
        console.log('Completed');
    });

// => 42
// => Completed
```

### Location

* rx.js

## *AsyncSubject Instance Methods* #\#

### `Rx.AsyncSubject.prototype.dispose()` <a href="#rxasyncsubjectprototypedispose" id="rxasyncsubjectprototypedispose"></a>

[#](/rxjs-book/summary/subjects/async_subject.md#rxasyncsubjectprototypedispose) [Ⓢ](https://github.com/Reactive-Extensions/RxJS/blob/master/src/core/subjects/asyncsubject.js#L111-L116)

Unsubscribe all observers and release resources.

#### Example

```javascript
var subject = new Rx.AsyncSubject();

var subscription = subject.subscribe(
    function (x) {
        console.log('Next: ' + x.toString());
    },
    function (err) {
        console.log('Error: ' + err);
    },
    function () {
        console.log('Completed');
    });

subject.onNext(42);
subject.onCompleted();

// => Next: 42
// => Completed

subject.dispose();

try {
    subject.onNext(56);
} catch (e) {
    console.log(e.message);
}

// => Object has been disposed
```

### Location

* rx.js

### `Rx.AsyncSubject.prototype.hasObservers()` <a href="#rxasyncsubjectprototypehasobservers" id="rxasyncsubjectprototypehasobservers"></a>

[#](/rxjs-book/summary/subjects/async_subject.md#rxasyncsubjectprototypehasobservers) [Ⓢ](https://github.com/Reactive-Extensions/RxJS/blob/master/src/core/subjects/asyncsubject.js#L49-L51)

Indicates whether the subject has observers subscribed to it.

#### Returns

*(Boolean)*: Returns `true` if the AsyncSubject has observers, else `false`.

#### Example

```javascript
var subject = new Rx.AsyncSubject();

console.log(subject.hasObservers());

// => false

var subscription = subject.subscribe(
    function (x) {
        console.log('Next: ' + x.toString());
    },
    function (err) {
        console.log('Error: ' + err);
    },
    function () {
        console.log('Completed');
    });

console.log(subject.hasObservers());

// => true
```

### Location

* rx.js
