The Empty, Never, and Throw operators generate Observables with very specific and limited behavior. These are useful for testing purposes, and sometimes also for combining with other Observables or as parameters to operators that expect other Observables as parameters.
RxGroovy implements these operators as empty
, never
, and
error
. The error
operator takes as a parameter the
Throwable
with which you want the Observable to terminate.
These operators do not operate by default on any particular Scheduler, but
empty
and error
optionally take a Scheduler as a parameter, and if
you pass them a Scheduler they will issue their termination notifications on that Scheduler.
println("*** empty() ***"); Observable.empty().subscribe( { println("empty: " + it); }, // onNext { println("empty: error - " + it.getMessage()); }, // onError { println("empty: Sequence complete"); } // onCompleted ); println("*** error() ***"); Observable.error(new Throwable("badness")).subscribe( { println("error: " + it); }, // onNext { println("error: error - " + it.getMessage()); }, // onError { println("error: Sequence complete"); } // onCompleted ); println("*** never() ***"); Observable.never().subscribe( { println("never: " + it); }, // onNext { println("never: error - " + it.getMessage()); }, // onError { println("never: Sequence complete"); } // onCompleted ); println("*** END ***");
*** empty() *** empty: Sequence complete *** error() *** error: error - badness *** never() *** *** END ***
empty()
never()
error(throwable)
RxJava implements these operators as empty
, never
, and
error
. The error
operator takes as a parameter the
Throwable
with which you want the Observable to terminate.
These operators do not operate by default on any particular Scheduler, but
empty
and error
optionally take a Scheduler as a parameter, and if
you pass them a Scheduler they will issue their termination notifications on that Scheduler.
empty()
never()
error(throwable)
RxJS implements these operators as empty
, never
, and
throw
.
var source = Rx.Observable.empty(); var subscription = source.subscribe( function (x) { console.log('Next: ' + x); }, function (err) { console.log('Error: ' + err); }, function () { console.log('Completed'); });
Completed
// This will never produce a value, hence never calling any of the callbacks var source = Rx.Observable.never(); var subscription = source.subscribe( function (x) { console.log('Next: ' + x); }, function (err) { console.log('Error: ' + err); }, function () { console.log('Completed'); });
var source = Rx.Observable.return(42) .selectMany(Rx.Observable.throw(new Error('error!'))); var subscription = source.subscribe( function (x) { console.log('Next: ' + x); }, function (err) { console.log('Error: ' + err); }, function () { console.log('Completed'); });
Error: Error: error!
empty
is found in the following distributions:
rx.js
rx.all.js
rx.all.compat.js
rx.compat.js
rx.lite.js
rx.lite.compat.js
never
is found in the following distributions:
rx.js
rx.compat.js
rx.lite.js
rx.lite.compat.js
throw
is found in the following distributions:
rx.js
rx.all.js
rx.compat.js
rx.lite.js
rx.lite.compat.js