Difference between EMPTY and of(null) from RxJS
Wednesday, December 2, 2020
Difference between EMPTY and of(null) from RxJS
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')
})
Have a question or comment?