Skip to content
richardszalay edited this page May 20, 2011 · 7 revisions

Defers selection of a sequence until the sequence is subscribed to

static function defer(observableFactory:Function) : IObservable.<TResult>

Where observableFactory is function () : IObservable.<TResult>

Remarks

The returned sequence completes if the source sequence completes

The returned sequence errors if the source sequence errors or if func throws an error

Marble Diagrams

xs ─┐
   f()
    └───o─────o──/
        │     │  │
zs ─────o─────o──/

Return Value

IObservable.<TResult>

Examples

var toggle : Boolean = false;

var source : IObservable = Observable.defer(function() : IObservable
{
    toggle = !toggle;
    return (toggle)
        ? Observable.value(50)
        : Observable.value(100);
});

var observer : IObserver = Observer.create(
        function(x:int) : void { trace(x); },
        function() : void { trace("Completed"); }
    );

source.subscribeWith(observer);
source.subscribeWith(observer);
source.subscribeWith(observer);

    // Trace output is:
    // 50
    // Completed
    // 100
    // Completed
    // 50
    // Completed
Clone this wiki locally