Async Subject

import { AsyncSubject } from 'rxjs';

let asyncSubject = new AsyncSubject();
asyncSubject.subscribe(
  (value) => console.log('async subject', value),
  (error) => console.error('async error', error),
  () => console.log('async completed')
);

asyncSubject.next( 1 );
asyncSubject.next( 2 );
1
2
3
4
5
6
7
8
9
10
11

Looking at this we expect 1,2 to be emitted right? WRONG. Nothing will be emitted unless complete() happen

asyncSubject.next( 3 )
asyncSubject.complete()

// emit 3
1
2
3
4

complete() needs to happen regardless of the finishing operation before it succeeds or fails so

asyncSubject( 3 )
asyncSubject.error('err')
asyncSubject.complete()

// will emit 'err' as the last action
1
2
3
4
5

Business case

When you care about preserving the last state just before the stream ends, be it a value or an error. So NOT last emitted state generally but last before closing time. With state I mean value or error.