How to use Async-Await in RxJS Observable in

How to RxJS ObservableuseAsync-Await

async-await Observables and not "used together" in general. But RxJS from the outset and have a high degree of interoperability Promises.

Why is there this need

Since NG7, previous work run into a more complex logic operation used (probably call each other when the two polling, polling each has a plurality of states, wherein a polling also built setTimeout, setInterval)

Rxjs directly lead to a lot of nesting (probably my rxjs write too much food, rxjs little difficult to learn, so long with only some basic usage or write great god gorgeous operations), in short, use rxjs deal with this logic, let me become very complex code readability becomes very low, leading to even look at the code behind themselves have become very difficult, and today I work hard, I decided to use Async-Awaitrewrite this part of asynchronous logic. Finally solved the problem of readability of the code, due to the high readability, but also solve some bug, and before a potential bug has been unable to solve.

application

Probably sum up how to RxJS Observableuse Async-Await;

// constructor an observation target 
const Source $ = Observable.interval (1000) .take (. 3 ); 

// The first step of course is to use the keyword asycn stated function 
asycn function Test () {
     the try { 

        // toPromise () will be may be converted to an observation target source $ Promise; 
        const X = the await source $ .toPromise (); 

        the console.log (X); 

    } the catch (ERR) { 
        the console.log (ERR); 
    } 
}

 

 

Guess you like

Origin www.cnblogs.com/siegaii/p/11247544.html
Recommended