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 );
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
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
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.