How to get a catch after retryWhen?

I want to repeat the request getseveral times with a second delay in case of an error, but if all attemps failed, execute the error handler.

After requesting re-request code, but catch is never executed. How can i fix this?

import {Response, Http} from '@angular/http';
import {Observable} from 'rxjs/Rx';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/map';
this.http.get("/api/getData").map(data => {
  console.log('get')
  return data.json()
})
.retryWhen(err => {
  console.log('retry')
  return err.delay(1000).take(5)
})
.catch(err => {
  console.log('catch')
  this.handleHttpError(err)
  return err
})
.subscribe(data => {
  console.log('subscribe')
  console.log(data)
})
+4
source share
2 answers

The problem is that when the Observable notification returned from the callback in retryWhensends a notification complete, which it then propagates as complete, which is not what you want from your description.

error, , take() .

, :

Observable.defer(() => Observable.throw("It broken"))
  .retryWhen(err => {
    console.log('retry');
    let retries = 0;
    return err
      .delay(1000)
      .map(error => {
        if (retries++ === 5) {
          throw error;
        }
        return error;
      });
  })
  .catch(err => {
    console.log('catch');
    return Observable.of(err);
  })
  .subscribe(data => {
    console.log('subscribe');
    console.log(data);
  });

retries , , . map() try-catch, , , error.

+2
return err.delay(1000).take(5)

, :

return err.delay(1000).take(5).concat(Observable.throw(err))

@martin, , Observable .

+2

Source: https://habr.com/ru/post/1674971/


All Articles