- 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設(shè)備驅(qū)動(dòng)開發(fā)詳解:基于最新的Linux4.0內(nèi)核
- Learning OpenDaylight
- Linux系統(tǒng)文件安全實(shí)戰(zhàn)全攻略
- Puppet實(shí)戰(zhàn)
- WordPress Mobile Web Development:Beginner's Guide
- 異質(zhì)結(jié)原理與器件
- 高性能Linux服務(wù)器構(gòu)建實(shí)戰(zhàn):系統(tǒng)安全、故障排查、自動(dòng)化運(yùn)維與集群架構(gòu)
- 8051軟核處理器設(shè)計(jì)實(shí)戰(zhàn)
- Linux使用和管理指南:從云原生到可觀測(cè)性
- Windows 7應(yīng)用入門與技巧
- 計(jì)算機(jī)應(yīng)用基礎(chǔ)(Windows 7+Office 2016)
- Linux操作系統(tǒng)
- Advanced Infrastructure Penetration Testing
- μC/OS-III內(nèi)核實(shí)現(xiàn)與應(yīng)用開發(fā)實(shí)戰(zhàn)指南:基于STM32
- Windows網(wǎng)絡(luò)編程(第2版)