The default_if_empty operator emits an item if the source observable is empty. The following figure shows the marble diagram of this operator:
Figure 9.29: The default_if_empty operator
Its prototype is the following:
Observable.default_if_empty(self, default_value=None)
Here, default_value is the value to emit if the source observable is empty. If no value is provided, then an item with the None value is emitted when the source observable is empty.
Here is an example of this operator:
empty = Observable.empty()
empty.default_if_empty("default").subscribe(
on_next = lambda i: print("on_next {}".format(i)),
on_error = lambda e: print("on_error: {}".format(e)),
on_completed = lambda: print("on_completed")
)
This example gives the following result: