Why is my promise not running synchronously?

后端 未结 2 1313
无人及你
无人及你 2021-01-24 17:15

Have looked around and can\'t quite find the answer. I am trying to run a promise which executes a fulfil statement once all facebook api pages have been iterated through and sa

相关标签:
2条回答
  • 2021-01-24 17:41

    You need to fullfil/resolve the value that you need from promise. Call the function fulfill with a value you that you need.For example if you want to resolve an array:

    fullfill(array);
    
    0 讨论(0)
  • 2021-01-24 17:53

    Here might be one way to do it since you are using get call, I've modified to not use promise at all because ajax returns the promise anyway.

    It is a pseudo code, you might have to tweak it a bit to get it running

    function pageThroughLikes(facebookPostArray) {
      var testArray = []
      var promsieList = []
        facebookPostArray.forEach(function(array) {
          array.forEach(function(innerObject) {
            if ('likes' in innerObject) {
              if ('paging' in innerObject.likes) {
                if ('next' in innerObject.likes.paging) {
                  nextPage = innerObject.likes.paging.next;
                  currentPostId = innerObject.id;
                  currentDataLength = innerObject.likes.data.length;
                  i = 0;
                  do{
                    promsieList.push(
                    $.ajax({url : nextPage                  
                    }))
                    i += 1;
                    } while (currentDataLength != 0 && i > 10)
                  }
               }
             }
          })
       });
       console.log('paged through likes')
       return promiseList();    
    }
    
    processData = function(nextLikePageData){
        likeData = {};
                      likeData.id = currentPostId;
                      likeData.likes = {};
                      likeData.likes.data = nextLikePageData.data
                      likeData.likes.paging = nextLikePageData.paging
                      console.log(likeData)
                      testArray.push(likeData);
                      facebookPostArray.push(testArray);
                      console.log('pushed to postArray')
      return likeData;
    }
    
    $(document).ready(function() {
      getPostLikes().then(function() {
      $.when.apply(this, pageThroughLikes(postArray)).done(function() {
        //debug to examine the arguments object, you'll notice its an array of arrays
        var testArray = []
        $.each(arguments, function(k, v){
            var dt = processData(v[0]);
            testArray.push(dt);
            facebookPostArray.push(dt);         
        });
        console.log(testArray)
        test(testArray); // OR
        test(facebookPostArray);
       });
     });
    });
    
    function test(postArray) {
      var convertedPostCSV = convertArrayOfObjectsToCSV(postArray);
      downloadCSV(convertedPostCSV);
    }
    

    EDIT:

    • The arguments is a default object returned by the jquery $.when function.
    • This is a collection of subarrays and each subarray consists of data, status & promise object.
    • I'm assuming that you need to consolidate the data from all the get calls into a testArray so, that is what is being done in the when call
    0 讨论(0)
提交回复
热议问题