Pages

    Monday, April 27, 2009

    JavaScript: Callbacks in Loops

    I just finished a mashup that had to be blogged about. I suffered to find this solution, and I wanted to share what I learned with the world.

    In the mashup I took a twitter feed and plotted the tweets onto a map based on the location of the tweeter. Let me set the stage.

    The Google Map has already been set up and the list of tweets has been obtained. It is now time to plot the tweets onto the map. This will be done within a function called addMarkers. The HTTP Geocoder that Google provides will be doing our geocoding. For more information on this service, see this.

    Keep in mind that I'm doing all of this in a Presto Mashlet, and will be calling out to the HTTP Geocoder via a URLProxy call that is undocumented but available for use.

    At first blush, the following approach seems appropriate. Here is an excerpt from the addMarkers function:



    However, this suffers from a very serious drawback, and that drawback revolves around the scope of the function as it exists on the stack. Remember that you are calling out and receiving an asynchronous response via the callback. There's no telling where this loop will be when a callback returns, but the scope of the function is maintained on the stack until all of the callbacks have been completed.

    When a callback returns, the current value of i will be used to index into tweets! Since all of these calls take time, the most common result is that i will actually be out of bounds of tweets. Recall that updating the loop variable is the last operation done in any JavaScript for loop. Once you have looped through all of your indexes you, of necessity, must set i to be out of bounds of tweets. Therefor, i will be equal with tweets.length.

    The result is that you pass an undefined object into placeMarker in place of what should have been the tweet.

    The next logical step is that you should create a variable to hold the value of i, like this:

    var myTweet = i;
    ...
    this.placeMarker(point, tweets[myTweet]);

    However, this will fail as well!

    The problem here is that myTweet is still within the scope of our addMarkers function. addMarkers will therefor have only one copy of myTweet. Once again, you end up in a situation where the loop will probably finish before any of the callbacks return. The net result this time, however, is slightly different. You will pass in a valid tweet to placeMarkers, but it will be the last tweet in every instance. You'll have the same tweet attached to all of your markers on the map, the last tweet in the list.

    So, how do you remove the timing issues? This is where I suffered. I hunted and pecked out half-solutions for quite a while. Finally, I had to start thinking outside of the normal box to come up with a solution.

    The whole problem revolves around all of the callbacks returning to a shared scope in the stack, that being the scope of addMarkers. Once you consider it that way, it becomes obvious that providing each callback with its own scope on the stack is what is needed. The way to do that is to have a function fire off the HTTP Geocoder request. The function will get its own spot on the stack and will have its own scope. Let addMarkers maintain the loop and call this function whenever it wants to fire off a request. Pass in the tweets and the desired value of i to be remembered.

    Consider the following:

    This approach will result in the correct tweet being displayed with the correct marker on the map.

    4 comments:

    Unknown said...

    You just saved our lives. THANK YOU MICHAEL COLLINS.

    Michael.Rollins said...

    No problem, azuriel. Glad to help!

    Unknown said...

    some good code!
    thanks for the hint of first calling a function before defining the callback function.
    this most probably saved me a lot of time!

    Michael.Rollins said...

    Patrick, so happy that you found the code and ideas useful! Keep up the good work.