angular2 Observable.throw doesn't work after Observable.timer

283 Views Asked by At

I'm trying to do a retrywhen on a http.get for X-times when communication fails.

After the X-times I would like to throw an error that it failed to connect.

If I only do in my DelayWhen an Observable.throw it directly works fine and returns on the first try. But if i for the first times return an Obersvable.timer, to delay the next to tries for connection afterwards the Observable.throw is called but it will not be forwarded to my upper level catch!?

public initializeCommunication(): Promise<boolean> {
 return new Promise<boolean>((resolve: (r: boolean) => void, reject: (e: 
  Error) => void) => {
 return this.getInternal<DTO>(this.Url, null, 
 'clientidentity')
.retryWhen((errors: Observable<any>) => {
                    // Retry upon communication failure
                    // TODO: Consider only retrying a limited number of 
                    //times and throw an error (call reject)

                    return errors.delayWhen((response: Response) => {
                        retrytrys++;
                        console.log('number of retries   ' + retrytrys);

                        if (retrytrys > 2) {
                            console.log('throw');
                            return Observable.throw(new Error('error!'));
                        }
                        console.log('ret');
                        return Observable.timer(this.RETRY_TIMEOUT);
                    });
                })
                .subscribe()
});
}

and this function I'm calling from another service and in that the catch is not called.

            myService.initializeCommunication().then((r: boolean) => {

            })
            .catch((error) => { //this is not called }
0

There are 0 best solutions below