DEV Community

Pavel Svitek
Pavel Svitek

Posted on

How to write a Backoff function in Typescript

Imagine you want to call your backend API (or other network operation) but it fails.

You would like to call it 2 or 3 times more with some delay. Much like how Gmail works when it tries to connect back after losing a connection.

The code below implements an async function that works just like that.

The code should be self-explainable, but feel free to ask in comments if anything is not clear.

The KUDOS for implementation of the original function backoffOrig() goes to Saša Savić
https://www.linkedin.com/feed/update/urn:li:activity:6655380458282594304

Discussion (0)