- 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:
- Implementing Cisco UCS Solutions
- Linux從零開始學(xué)(視頻教學(xué)版)
- 精解Windows 8
- 嵌入式操作系統(tǒng)(Linux篇)(微課版)
- Windows 7中文版從入門到精通(修訂版)
- Linux內(nèi)核觀測技術(shù)BPF
- Linux內(nèi)核設(shè)計的藝術(shù):圖解Linux操作系統(tǒng)架構(gòu)設(shè)計與實現(xiàn)原理
- Learn CUDA Programming
- Advanced Infrastructure Penetration Testing
- Learn SwiftUI
- UI設(shè)計手繪表現(xiàn)從入門到精通
- Ubuntu Linux操作系統(tǒng)實用教程
- Drupal 7 Cookbook
- Web Penetration Testing with Kali Linux(Third Edition)
- OpenHarmony開發(fā)與實踐:基于紅莓RK2206開發(fā)板