Defer

do not create the Observable until the observer subscribes, and create a fresh Observable for each observer

Defer

The Defer operator waits until an observer subscribes to it, and then it generates an Observable, typically with an Observable factory function. It does this afresh for each subscriber, so although each subscriber may think it is subscribing to the same Observable, in fact each subscriber gets its own individual sequence.

In some circumstances, waiting until the last minute (that is, until subscription time) to generate the Observable can ensure that this Observable contains the freshest data.

See Also:

Language-Specific Information:

TBD

defer

RxGroovy implements this operator as defer. This operator takes as its sole parameter an Observable factory function of your choosing. This function takes no parameters and returns an Observable.

defer does not by default operate on a particular Scheduler.

switchCase

There is a somewhat similar operator in the optional rxjava-computation-expressions package (it is not part of the standard RxGroovy set of operators). The switchCase operator conditionally creates and returns one of a set of possible Observables.

ifThen

An even simpler operator in the optional rxjava-computation-expressions package (also not part of the standard RxGroovy set of operators) is ifThen. This operator checks a condition and then either mirrors the source Observable or an empty Observable depending on the result.

defer

RxJava implements this operator as defer. This operator takes as its sole parameter an Observable factory function of your choosing. This function takes no parameters and returns an Observable.

defer does not by default operate on a particular Scheduler.

switchCase

There is a somewhat similar operator in the optional rxjava-computation-expressions package (it is not part of the standard RxJava set of operators). The switchCase operator conditionally creates and returns one of a set of possible Observables.

ifThen

An even simpler operator in the optional rxjava-computation-expressions package (also not part of the standard RxGroovy set of operators) is ifThen. This operator checks a condition and then either mirrors the source Observable or an empty Observable depending on the result.

defer

RxJS implements this operator as defer. This operator takes as its sole parameter an Observable factory function of your choosing. This function takes no parameters and returns an Observable or a Promise.

Sample Code

/* Using an observable sequence */
var source = Rx.Observable.defer(function () {
    return Rx.Observable.return(42);
});

var subscription = source.subscribe(
    function (x) { console.log('Next: ' + x); },
    function (err) { console.log('Error: ' + err); },
    function () { console.log('Completed'); } );
Next: 42
Completed
var source = Rx.Observable.defer(function () {
    return RSVP.Promise.resolve(42);
});

var subscription = source.subscribe(
    function (x) { console.log('Next: ' + x); },
    function (err) { console.log('Error: ' + err); },
    function () { console.log('Completed'); } );
Next: 42
Completed

defer 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
if

RxJS also implements the if operator. It takes as parameters a function that returns a boolean, an Observable to mirror if that function returns a true value, and optionally a second Observable to mirror if that function returns a false value (if you omit this parameter, if will mirror an empty Observable in such a case).

Sample Code

var shouldRun = false;

var source = Rx.Observable.if(
    function () { return shouldRun; },
    Rx.Observable.return(42),
    Rx.Observable.return(56)
);

var subscription = source.subscribe(
    function (x) { console.log('Next: ' + x); },
    function (err) { console.log('Error: ' + err); },
    function () { console.log('Completed'); });
Next: 56
Completed

if is found in the following distributions:

  • rx.all.js
  • rx.experimental.js (requires rx.js, rx.compat.js, rx.lite.js, or rx.lite.compat.js)
case

RxJS implements a somewhat similar operator called case (or “switchCase”). This operator conditionally creates and returns one of a set of possible Observables. It takes the following parameters:

  1. a function that returns the key that determines which Observable to emit
  2. an object that associates those keys with particular Observables
  3. (optional) a Scheduler or an Observable:
    Scheduler
    the Scheduler you want this operator to use
    Observable
    the default Observable to emit if the key does not associate with any Observables

Sample Code

var sources = {
    'foo': Rx.Observable.return(42),
    'bar': Rx.Observable.return(56)
};

var defaultSource = Rx.Observable.empty();

var source = Rx.Observable.case(
    function () {
        return 'foo';
    },
    sources,
    defaultSource);

var subscription = source.subscribe(
    function (x) { console.log('Next: ' + x); },
    function (err) { console.log('Error: ' + err); },
    function () { console.log('Completed'); } );
Next: 42
Completed

case/switchCase is found in the following distributions:

  • rx.all.js
  • rx.all.compat.js
  • rx.experimental.js (requires rx.js, rx.compat.js, rx.lite.js, or rx.lite.compat.js)

RxPHP implements this operator as defer.

Returns an observable sequence that invokes the specified factory function whenever a new observer subscribes.

Sample Code

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


$source = \Rx\Observable::defer(function () {
    return \Rx\Observable::of(42);
});

$subscription = $source->subscribe($stdoutObserver);

   
Next value: 42
Complete!