Search code examples
jquerysharepoint-2007

I expect sequential results from a SharePoint jQuery app but get random results


I retrieve a list of email documents (*.eml files) from a SharePoint library. When that list is complete, I iterate through it, putting some file access data into an array of arrays. At the end of this process, I have an array of email file names and other access data. I iterate through this list, opening each email file via Ajax (jQuery). When the email content has been returned to me (from a non-Ajax decoding routine), I send it out for processing.

This processing is sequentially dependent. I get email1 from the access array, I send it for decoding, and then I process it (essentially, write it to a list). The problem I'm having is that although I send out email1, then email2, then email3, what I get back from the decoder is whatever it feels like returning. Maybe 1,2,3, maybe 1,3,2, maybe 3,2,1. Because I was careful to ensure that I process the email sequentially, I expected to get its content back sequentially. Nope.

    $.each(arIDs, function(index, value) {
        //the following statement orders perfectly
          $("#results").append("<br>dealing with " + arIDs[index][2]);
          var ajaxPromise = $.ajax({
            type:"GET",
            url:"GFSSEForm/" + arIDs[index][2],
            dataType:"text"
        }).done(function(data) {
            decodeEmail(data);  //sets global arValues
            $("#results").append("<br>" + arValues);  //not returned in order passed
        });
.
.
.

Any ideas?


Solution

  • I did extensive tests against a test site I created (http://www.michaelbroschat.com/Ajax/AjaxTest.html), and I eventually got all browsers I tried to fail (ie, to queue in other than a FIFO sequence). Modern browsers were best (IE10, Firefox, Chrome) but all eventually failed. My work environment is IE7, and I also tested on IE8. Both fail more quickly than do modern browsers.

    I guess that the bottom line is that you cannot be absolutely sure that your browser will queue its events in the order they happen.