0

I've been trying to re-write my code all day and keep ending up in the same place. When I add console logs to this request it seems that although I have set async: false the code is still running asynchronously.

    var likePageId = facebookData.id;
    var postArray = [];
    var postIdArray = [];
    var testArray = [];
    var check = 0;

    if (facebookData.hasOwnProperty('posts')) {
      facebookData.posts.data.forEach(function(post) { 
        if ('likes' in post) { 
          if ('paging' in post.likes) { 

            post.likes.data.forEach(function(like) { /
              testArray.push(like.id + ' ' + post.id); 
            });
            nextPage = post.likes.paging.next; 
            var i = 0;
            do {
              $.ajax({ 
                async: false,
                type: 'GET',
                url: nextPage,
                success: function(newPageData) { 
                  newPageData.data.forEach(function(like) { 
                    testArray.push(like.id + ' ' + post.id); 
                  });
                  if ('paging' in newPageData.data) { 
                    if ('next' in newPageData.data.paging) {
                      nextPage = newPageData.paging.next; 
                      console.log('NEXT PAGE REASSIGNED')
                    }
                  }
                  currentDataLength = newPageData.data.length;
                  console.log('CURRENT DATA LENGTH! ' + currentDataLength)
                  console.log(i);
                }
              });
              i += 1;
            } while (currentDataLength != 0 && i < 5);
          }
          console.log(post.likes)
        }
      })
      postArray.push(facebookData.posts.data);`

My current aim is to take data from a specified facebook pages posts. Once I have worked out how to paginate through all of the likes for a specific post, I would like to paginate through all of the posts (since a specified date) and paginate through their likes, sending User_id, post_id, page_id to a CSV file.

  • 3
    Because synchronous ajax for the most part have been deprecated, as it's an obomination – adeneo Nov 16 '16 at 16:17
  • What version of jQuery are you using? The documentation pretty explicitly states that as of 1.8 the use of `async:false` with `jqXHR` has been deprecated. – David Nov 16 '16 at 16:20
  • Ok cool. Thanks for letting me know, didn't realise! – LastMan0nEarth Nov 16 '16 at 16:28
  • Any pointers on how to clean this up @David? Pretty new to this level of coding but really want to have a good go at it. – LastMan0nEarth Nov 16 '16 at 16:30
  • 1
    @LastMan0nEarth: That's a bit broad, to be honest. Basically when you're designing the code you should be responding to asynchronous operations in their callbacks rather than trying to force those operations to be synchronous. – David Nov 16 '16 at 16:32
  • Ok thanks, this has helped massively! I've got much closer to completing by reading up on my callbacks. Have also extracted a bunch of code etc so its a bit neater. – LastMan0nEarth Nov 18 '16 at 10:42

0 Answers0