Wait for an async operation in onNext of RxJS Observable

前端 未结 3 910
暖寄归人
暖寄归人 2021-02-02 08:15

I have an RxJS sequence being consumed in the normal manner...

However, in the observable \'onNext\' handler, some of the operations will complete synchronously, but oth

3条回答
  •  囚心锁ツ
    2021-02-02 09:09

    Each operation you want to perform can be modeled as an observable. Even the synchronous operation can be modeled this way. Then you can use map to convert your sequence into a sequence of sequences, then use concatAll to flatten the sequence.

    someObservable
        .map(function (item) {
            if (item === "do-something-async") {
                // create an Observable that will do the async action when it is subscribed
                // return Rx.Observable.timer(5000);
    
                // or maybe an ajax call?  Use `defer` so that the call does not
                // start until concatAll() actually subscribes.
                return Rx.Observable.defer(function () { return Rx.Observable.ajaxAsObservable(...); });
            }
            else {
                // do something synchronous but model it as an async operation (using Observable.return)
                // Use defer so that the sync operation is not carried out until
                // concatAll() reaches this item.
                return Rx.Observable.defer(function () {
                    return Rx.Observable.return(someSyncAction(item));
                });
            }
        })
        .concatAll() // consume each inner observable in sequence
        .subscribe(function (result) {
        }, function (error) {
            console.log("error", error);
        }, function () {
            console.log("complete");
        });
    

    To reply to some of your comments...at some point you need to force some expectations on the stream of functions. In most languages, when dealing with functions that are possibly async, the function signatures are async and the actual async vs sync nature of the function is hidden as an implementation detail of the function. This is true whether you are using javaScript promises, Rx observables, c# Tasks, c++ Futures, etc. The functions end up returning a promise/observable/task/future/etc and if the function is actually synchronous, then the object it returns is just already completed.

    Having said that, since this is JavaScript, you can cheat:

    var makeObservable = function (func) {
        return Rx.Observable.defer(function () {
            // execute the function and then examine the returned value.
            // if the returned value is *not* an Rx.Observable, then
            // wrap it using Observable.return
            var result = func();
            return result instanceof Rx.Observable ? result: Rx.Observable.return(result);
        });
    }
    
    someObservable
        .map(makeObservable)
        .concatAll()
        .subscribe(function (result) {
        }, function (error) {
            console.log("error", error);
        }, function () {
            console.log("complete");
        });
    

提交回复
热议问题