jQuery promises and exponential backoff

When using services like Google Fusion API, sometimes you get errors because of over quota attempts – too many requests in too short a time, or the infrastructure is just too busy to service them. Google recommend you implement Exponential Backoff.

The idea is quite simple – slow down your requests by increasing amounts of random time, and eventually give up. A request that’s worth trying again will return an error 503 with various specific messages – often something like ‘backend error’.

I tend to use jQuery promises to deal with asynchronous requests. This makes implementing exponential backoff rather trivial. Here’s an extract from d3 integration with Google Fusion Tables which uses promises to make multiple simultaneous requests to fusion. To report progress, I use a traffic light system – the ‘light’ argument just defines the which div to show the progress light in.

 

And that’s all there is to it. The caller can simply wait for the promise to be resolved or rejected, and the backoff will be executed a few times as per the best practice recommendation.

About brucemcp 223 Articles
I am a Google Developer Expert and decided to investigate Google Apps Script in my spare time. The more I investigated the more content I created so this site is extremely rich. Now, in 2019, a lot of things have disappeared or don’t work anymore due to Google having retired some stuff. I am however leaving things as is and where I came across some deprecated stuff, I have indicated it. I decided to write a book about it and to also create videos to teach developers who want to learn Google Apps Script. If you find the material contained in this site useful, you can support me by buying my books and or videos.

Be the first to comment

Leave a Reply

Your email address will not be published.


*


five × 2 =