Search code examples
javascriptjqueryajaxcallbackjquery-callback

How can I eliminate this ugly duplicate code during a retry-Ajax scenario?


Due to the callback-invoking nature of getGamesByPlayerId (which happens to be an Ajax call), I can't seem to figure out how to eliminate the duplicate code in the following:

// Load the player's games.
gc.api.getGamesByPlayerId(gc.game.player.id, gc.game.player.access_token, function(data) {

    if(data.status_code === 401) {

        // Call may have failed due to being called too fast. Retry...
        gc.api.getGamesByPlayerId(gc.game.player.id, gc.game.player.access_token, function(data) {

            if(data.status_code === 401) {

                // Call may have failed due to being called too fast. Retry...
                gc.api.getGamesByPlayerId(gc.game.player.id, gc.game.player.access_token, function(data) {

                    if(data.status_code === 401) {

                        // Call may have failed due to being called too fast. Retry...
                        gc.api.getGamesByPlayerId(gc.game.player.id, gc.game.player.access_token, function(data) {

                            if(data.status_code === 401) {

                                // OK. It's safe to assume the server is current, and that
                                // we truly are not authorized to do this.
                                alert("You are not authorized.");

                            } else {

                                // Add games to HTML.
                                for( var i = 0; i < data.length; i++ ) {

                                    var html = '<li><a href="?g=' + data[i].id + '">' + data[i].id + '</a></li>';

                                    $('#games').append(html);

                                }

                            }

                        });

                    } else {

                        // Add games to HTML.
                        for( var i = 0; i < data.length; i++ ) {

                            var html = '<li><a href="?g=' + data[i].id + '">' + data[i].id + '</a></li>';

                            $('#games').append(html);

                        }

                    }

                });

            } else {

                // Add games to HTML.
                for( var i = 0; i < data.length; i++ ) {

                    var html = '<li><a href="?g=' + data[i].id + '">' + data[i].id + '</a></li>';

                    $('#games').append(html);

                }

            }

        });

    } else {

        // Add games to HTML.
        for( var i = 0; i < data.length; i++ ) {

            var html = '<li><a href="?g=' + data[i].id + '">' + data[i].id + '</a></li>';

            $('#games').append(html);

        }

    }

});

Normally, I would think to use a for-loop, but that will not work because I don't want to fire off the Ajax calls in quick succession. I want the retry to fire only if the preceding call fails.


Solution

  • Ignoring the circumstances for which you would need to make the same request multiple times in a row, you could probably accomplish this with the use of a recursive function. For example, something like:

    loadPlayerGames(4);
    
    function loadPlayerGames(triesLeft) {
        gc.api.getGamesByPlayerId(gc.game.player.id, gc.game.player.access_token, function(data) {
            if(data.status_code !== 401) {
                // Add games to HTML.
                for( var i = 0; i < data.length; i++ ) {
                    var html = '<li><a href="?g=' + data[i].id + '">' + data[i].id + '</a></li>';
                    $('#games').append(html);
                }
            } else if(triesLeft <= 0) {
                // OK. It's safe to assume the server is current, and that
                // we truly are not authorized to do this.
                alert("You are not authorized.");
            } else {
                // Call may have failed due to being called too fast. Retry...
                loadPlayerGames(triesLeft - 1);
            }
        });
    }