Trying again

I’ve been doing some back-end work lately in which a lot of external services have to be communicated with over the network. The combination of external services and Network IO is waiting for the occasional hiccup to happen so we have the habit of trying things multiple times before giving up. In Scala we came up with the fairly straight-forward retry function that goes something like this:

As an ‘exercise for the reader’ I made something similar and very basic for our Javascript based front-ends. I know that there will probably be (better) libraries out there but hey, after the left-pad chaos we all know that writing your own utils from time to time is good for you.

It goes without saying that this is a very rudimentary setup very much focused at retrying failed network calls. Expanding it with a retry scheduling strategy and onTryFailed like logging side effect functions would be obvious next steps.

I will definitely not use this for every API call because sometimes we just want to fail fast(or check that we’re online before trying). I do think though that it might be very useful for (web)applications with a focus on mobile devices. It’s always a bit of a disappointment to see an app that’s not able to recover from a three seconds loss of internet connection when sitting in a train.

I should end with the note to self that it would be very interesting to inspect how these nested and possible scheduled promises are handled by the event loop and (micro)task queues.