RxJS: Retry With Delay — You’ll Want to Build This Operator

RxJS: Retry With Delay — You’ll Want to Build This Operator

While adventuring through the lands of RxJS, I’ve often found myself thirsting for the same operator. Retrying with delay seems like a commonly needed pattern. But is it nowhere to be found in RxJS? RxJS: Retry With Delay — You’ll Want to Build This Operator

What should the operator do?

It should retry x times with a delay of y. I came up with these explicit criteria while reviewing other approaches:

  1. Retries x times if an error is thrown.
  2. Has a delay of y before each retry.
  3. Emits like it wasn’t there if no error occurs.
  4. Emits the last thrown error in case x tries are exceeded. A lot of solutions were struggling especially with this.

What will we use to build the operator?

We’ll put together 4 existing RxJS operators to build our own retryWithDelay operator:

  • retryWhen: Mirrors the source observable. Maps errors into the notifier function which is provided as a parameter. If the notifier function emits a value the operator resubscribes to the source observable. Errors and completions of the notifier function are forwarded.
  • scan: Applies an accumulator function to emitted values. Provides access to the last value of the accumulator function.
  • tap: Perform a side effect for every item.
  • delay: Delay the received items by a given timeout.

How can we build our custom operator?

We’ll pipe the input observable into the retryWhen operator. In there we reuse the other 3 operators:

  1. First, we count the times the operator retried using scan. We’ll increase the count each time by one. We also overwrite the error attribute each time and keep track of the last error:
export function retryWithDelay<T>(
      delay: number,
      count = 1
    ): MonoTypeOperatorFunction<T> {
      return (input) =>
          retryWhen((errors) =>
              scan((acc, error) => ({ count: acc.count + 1, error }), {
                count: 0,
                error: undefined as any,

javascript programming angular typescript rxjs

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

JavaScript Vs TypeScript

Get to know here difference between JavaScript & TypeScript, In this blog explained with pros and cons of TypeScript & JavaScript.

Angular RxJs Operators: DebounceTime, RetryWhen and SwitchMap

Before jumping into rx-js operators we have to understand two of the most fundamental concepts which are observable and pipe in rx-js

Install Angular - Angular Environment Setup Process

Install Angular in easy step by step process. Firstly Install Node.js & npm, then Install Angular CLI, Create workspace and Deploy your App.

What is TypeScript? Why TypeScript? and Why Not TypeScript?

TypeScript extends JavaScript by adding Types. There are many great reasons to switch to TypeScript. Especially if your team uses JavaScript. There are some reasons to not use TypeScript as there are with any language or framework.

Basics of Angular: Part-1

What is Angular? What it does? How we implement it in a project? So, here are some basics of angular to let you learn more about angular. Angular is a Typesc