- Hands-On Reactive Programming with Python
- Romain Picard
- 256字
- 2021-06-24 18:25:24
The never and throw operators
The empty operator was discussed in Chapter 3, Functional Programming with ReactiveX. This operator is one of a triplet of similar operators: empty, never, and throw. The behavior of each of these operators should be clear from their names, as follows:
- The empty operator creates an empty observable that completes immediately
- The never operator creates an observable that never completes
- The throw operator creates an observable that immediately completes on error
The following figure shows the marble diagram of the never operator:
The following figure shows the marble diagram of the throw operator:

Their prototypes are as follows:
Observable.never()
Observable.throw(exception, scheduler=None)
Note that the never operator does not have a scheduler parameter. Since it never emits anything, there is no need for scheduling! The throw operator accepts any object in the exception parameter. If the provided value is an exception, then it is sent as-is. If the provided value is not an exception, then this value is encapsulated in an exception object before being notified. So, the observer of this observable will always receive an exception in its on_error handler.
The following code shows two ways to use the throw operator:
error = Observable.throw("Something wrong happened")
error.subscribe(
on_next=lambda i: print("item: {}".format(i)),
on_error=lambda e: print("error: {}".format(e)),
on_completed=lambda: print("completed")
)
exception = Observable.throw(NotImplementedError("I do nothing"))
exception.subscribe(
on_next=lambda i: print("item: {}".format(i)),
on_error=lambda e: print("error: {}".format(e)),
on_completed=lambda: print("completed")
)
The preceding code will provide the following results:
- Linux設備驅動開發詳解:基于最新的Linux4.0內核
- Social Media Mining with R
- PLC控制系統應用與維護
- Extending Bootstrap
- 嵌入式操作系統(Linux篇)(微課版)
- Linux就該這么學
- Kubernetes從入門到實踐
- Linux自動化運維:Shell與Ansible(微課版)
- Linux使用和管理指南:從云原生到可觀測性
- 網絡操作系統教程:Windows Server 2016管理與配置
- AutoCAD 2014中文版從入門到精通
- INSTANT Migration from Windows Server 2008 and 2008 R2 to 2012 How-to
- Windows 7使用詳解(修訂版)
- 統信UOS應用開發進階教程
- openEuler操作系統核心技術與行業應用實踐