W3cubDocs

/ReactiveX

Just

create an Observable that emits a particular item
Just

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.

See Also

Language-Specific Information

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.

Sample Code

// 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 );
  • Javadoc: 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.

Sample Code

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.");
        }
    });
Next: 1
Next: 2
Next: 3
Sequence complete.
  • Javadoc: just(item) (there are also versions that accept between two and nine items as parameters)
just

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.

Sample Code

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'); });
Next: 42
Completed

return/just is found in each of the following distributions:

  • rx.js
  • rx.compat.js
  • rx.lite.js
  • rx.lite.compat.js

RxPHP implements this operator as of.

Returns an observable sequence that contains a single element.

Sample Code

//from https://github.com/ReactiveX/RxPHP/blob/master/demo/of/of.php


$source =  \Rx\Observable::of(42);

$subscription = $source->subscribe($stdoutObserver);
Next value: 42
Complete!

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.

Sample Code

let source = Observable.just(1, 2, 3)

source.subscribe {
    print($0)
}

let source2 = Observable.just([1,2,3])

source2.subscribe {
    print($0)
}
next((1, 2, 3))
completed
next([1, 2, 3])
completed

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