lookout.devlookout.dev
search
Share Knowledge
10

Difference between EMPTY and of(null) from RxJS

Wednesday, December 2, 2020

EMPTY is an Observable that will complete immediately without emitting anything. The difference is of(null) will still emit null. The problem developers run into with EMPTY is that they expect their logic to run inside of the observer.next() block but because EMPTY never emits, observer.next() never gets invoked, observer.complete() does. The equivalence to EMPTY is of() (no arguments)

Code Examples

Difference between of(null) and EMPTY

EMPTY.subscribe({
  next: () => console.log('you will not see me'),
  complete: () => console.log('you will see me')
});

of(null).subscribe({
  next: () => console.log('you will see me and the complete guy down there'),
  complete: () => console.log('you will see me and the next guy up there')
})
Chau Tran

Software Engineer @swimlane | Core Team @nestjs | Author @nartc/automapper

Have a question or comment?