Hands-On Reactive Programming with Python
上QQ阅读APP看书,第一时间看更新

The empty operator

The empty operator returns an observable that completes immediately without sending any item:

Figure 3.6: The empty operator

The prototype of the empty operator is the following one:

Observable.empty(scheduler=None)

The empty operator can be used in the following way:

empty = Observable.empty()

empty.subscribe(
on_next=lambda i: print("item: {}".format(i)),
on_error=lambda e: print("error: {}".format(e)),
on_completed=lambda: print("completed")
)

The empty operator is useful mainly for testing and for stubbing:

  • During unit testing, it can be used to test components against empty observables as input. 
  • When implementing a function step by step, for example when following a test-driven development process, an initial implementation can be done with empty observables.