简体   繁体   中英

Rxjs: retry a promise?

Wonder why my promise is resolving but attempting to retry.

var getResultsStream = url => Rx.Observable.onErrorResumeNext( 
     Rx.Observable.defer( () => Rx.Observable
        .fromPromise( getSearchResults(url)
        .catch(error => Rx.Observable.of(`Error: ${error}`)) )
        .timeout(20000, new Error(`Timeout: ${url}`))
     )
     .do( e => console.log(`Retrying: ${url}`))
     .retry(3)
)

Is it a better way to retry a promise 3 times?

Unless you have a special use for the defer and/or the onErrorResumeNext , you could throw that all out and simply use:

 const request = url => Rx.Observable.of(url) .do(url => console.log("requesting: " + url)) .switchMap(url => Rx.Observable.fromPromise(getSearchResults(url))) .timeout(20000, new Error(`Timeout: ${url}`)) .retry(3); request("http://foobar.com").subscribe(console.log, console.error); function getSearchResults(url) { // simulating request-error throw new Error("Could not reach: " + url); } 
 <script src="https://npmcdn.com/@reactivex/rxjs@5.0.0-beta.10/dist/global/Rx.umd.js"></script> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM