javascriptjqueryajaxgiphy

Giphy API Call Returning Results out-of-order


I'm using an API call to Giphy to loop through a string array and return Gifs for each word in the string.

It's working, but the results are showing up out of order.

The beginning of the array is: "STATELY, PLUMP BUCK MULLIGAN CAME FROM THE STAIRHEAD"

And the results show like: Plump Mulligan Came Buck... you get the idea...

Here's the code:

    var chapter = "STATELY, PLUMP BUCK MULLIGAN CAME FROM THE STAIRHEAD," 

let words = chapter.split(" ");

for (i=0; i<words.length; i++){

    let word = words[i];

    var queryURL = "http://api.giphy.com/v1/gifs/search?q=" + word + "&api_key=<MY API KEY>";
    $.ajax({
        url: queryURL,
        method: 'GET'
        })
        .done(function(response) {


                var results = response.data;

                    var gifDiv = $('<div/>');
                    var gif = $('<img/>');
                    gif.addClass('myImg');
                    gif.attr('src', results[0].images.fixed_height.url);
                    gif.attr('data-animate', results[0].images.fixed_height.url)
                    gif.attr('data-state', 'still');
                    gifDiv.append(gif);

                    gifDiv.append('<h1>' + word + '</h1>');

                    gifDiv.appendTo($('#gifs-here'));

  })
};

any ideas why the results are out of order?


Solution

  • The two options

    Make all the requests as fast as possible, but process the results in series

    $.when(...chapter.split(" ").map(word => {
        var queryURL = "http://api.giphy.com/v1/gifs/search?q=" + word + "&api_key=<MY API KEY>";
        return $.ajax({
            url: queryURL,
            method: 'GET'
        });
    })).then((...responses) => {
        responses.forEach(response => {
            var results = response.data;
            ..... rest of your done code .....
        });
    })
    

    Make the requests and process the results in series, each request waiting for the previous processing ot complete

    chapter.split(" ").reduce((p, word) => {
        return p.then(() => {
           var queryURL = "http://api.giphy.com/v1/gifs/search?q=" + word + "&api_key=<MY API KEY>";
            return $.ajax({
                url: queryURL,
                method: 'GET'
            }).then(response => {
                var results = response.data;
                ..... rest of your done code .....
            });
        });
    }, $.when())
    

    For completeness, since jQuery $.ajax returns a thenable ... you can use Promises

    Promise.all(chapter.split(" ").map(word => {
        var queryURL = "http://api.giphy.com/v1/gifs/search?q=" + word + "&api_key=<MY API KEY>";
        return $.ajax({
            url: queryURL,
            method: 'GET'
        });
    })).then(responses => {
        responses.forEach(response => {
            var results = response.data;
            ..... rest of your done code .....
        });
    })
    

    and

    chapter.split(" ").reduce((p, word) => {
        return p.then(() => {
           var queryURL = "http://api.giphy.com/v1/gifs/search?q=" + word + "&api_key=<MY API KEY>";
            return $.ajax({
                url: queryURL,
                method: 'GET'
            }).then(response => {
                var results = response.data;
                ..... rest of your done code .....
            });
        });
    }, Promise.resolve());