Javascript closures as a way to retain state

              ·

Long time no blog! Let's get back into it with a nifty and clean way of retaining state in Javascript - closures. I was recently looking for an easy way to call a specific function after two separate/unrelated AJAX calls to two remote endpoints have been completed. The naive method would be to make the first AJAX call -> callback to the second AJAX call -> callback to doSomething, but we can use the fact that these two AJAX calls are not related and run them concurrently.


Bits of javascript goodness

             

(This blog post is better followed with the associated github repo. Run the Sinatra server with ‘ruby slowserver’, pop up a browser, and follow the revisions to see how the code gradually gets better. :) ) Recently at work, we wanted to modify some js ad code to include weather data for better ad targeting. For certain caching reasons, weather data has to be fetched by an AJAX call, then fed to the ad code.