create an Observable that emits a particular item
The Just operator converts an item into an Observable that emits that item.
Just is similar to From, but note that From will dive into an array or an iterable or something of that sort to pull out items to emit, while Just will simply emit the array or iterable or what-have-you as it is, unchanged, as a single item.
Note that if you pass null
to Just, it will return an Observable that emits null
as an item. Do not make the mistake of assuming that this will return an empty Observable (one that emits no items at all). For that, you will need the Empty operator.
just
RxGroovy implements this operator as just
. It accepts between one and nine items as parameters, and returns an Observable that emits these items in the same order as they are given in the parameter list.
just
does not by default operate on any particular Scheduler.
// Observable emits "some string" as a single item def observableThatEmitsAString = Observable.just("some string"); // Observable emits the list [1, 2, 3, 4, 5] as a single item def observableThatEmitsAList = Observable.just([1, 2, 3, 4, 5]); // Observable emits 1, 2, 3, 4, and 5 as distinct items def observableThatEmitsSeveralNumbers = Observable.just( 1, 2, 3, 4, 5 );
just(item)
(there are also versions that accept between two and nine items as parameters)just
RxJava implements this operator as just
. It accepts between one and nine items as parameters, and returns an Observable that emits these items in the same order as they are given in the parameter list.
just
does not by default operate on any particular Scheduler.
Observable.just(1, 2, 3) .subscribe(new Subscriber<Integer>() { @Override public void onNext(Integer item) { System.out.println("Next: " + item); } @Override public void onError(Throwable error) { System.err.println("Error: " + error.getMessage()); } @Override public void onCompleted() { System.out.println("Sequence complete."); } });
just(item)
(there are also versions that accept between two and nine items as parameters)just return
RxJS implements this operator as return
and as just
(two names for the same operator with the same behavior). It accepts a single item as a parameter and returns an Observable that emits that single item as its sole emission.
return
/just
operates by default on the immediate
Scheduler, but you can also pass in a Scheduler of your choosing as an optional second parameter, in which case it will operate on that Scheduler instead.
var source = Rx.Observable.just(42); var subscription = source.subscribe( function (x) { console.log('Next: %s', x); }, function (err) { console.log('Error: %s', err); }, function () { console.log('Completed'); });
return
/just
is found in each of the following distributions:
rx.js
rx.compat.js
rx.lite.js
rx.lite.compat.js
of
just sequenceOf
In Swift, this is implemented using the Observable.just
class method.
The parameter, whether a tuple (i.e. (1, 2, 3)
) or an array (i.e. [1,2,3]
) is produced as one emission.
let source = Observable.just(1, 2, 3) source.subscribe { print($0) } let source2 = Observable.just([1,2,3]) source2.subscribe { print($0) }
The difference between this and Observable.from
is that the latter's parameter is an array or a sequence, and emits each of its element as one emission.
© ReactiveX contributors
Licensed under the Apache License 2.0.
http://reactivex.io/documentation/operators/just.html