Working with Ionic 3 (Ionic 4 still sucks for production apps) I had this weird requirement to subscribe to one Observable and in case of error, wait some time, repeat the action twice and if it fails again, catch the error and do some stuff.
First I thought it’s really easy to do this with RxJs and it seems it is. I haven’t had the time to properly test this but here it is, my little function that transforms an Observable to a retriable one.
export function retry(o: Observable, count = 2, delayMs = 2000): Observable { let retries = 0; return o.retryWhen(err => { return err .delay(delayMs) .map(error => { retries++; if (retries >= count) { throw error; } return error; }); }); }
Maybe it’s useful for you too, for me it’s good enough 🙂