The do_action operator

The do_action operator allows us to execute a function each time an item is emitted on the source observable. This operator emits the same items as those received as input. The following figure shows the marble diagram of this operator:

Figure 9.18: The do_action operator

Its prototype is the following:

Observable.do_action(self, 
on_next=None,
on_error=None,
on_completed=None,
observer=None)

Here, on_next, on_error, and on_completed are functions called each time the following occur:

  • An item is received from the source observable
  • An error is received from the source observable
  • The source observable completes

The observer parameter can be provided to forward all these events to observer.

Here is an example of the do_action operator:

numbers = Observable.from_([1, 2, 3])

numbers.do_action(lambda i: print("action")).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:

action
on_next 1
action
on_next 2
action
on_next 3
on_completed
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.123.34