RxJs – Retry with delay and error handling


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
            .map(error => {
                if (retries >= count) {
                    throw error;
                return error;

Maybe it’s useful for you too, for me it’s good enough 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s