为什么我的承诺没有同步运行?

时间:2016-11-21 14:54:00

标签: javascript facebook-graph-api asynchronous concurrency promise

环顾四周,找不到答案。我试图运行一个承诺,一旦所有facebook api页面被迭代并保存到数组,就执行一个履行声明。

我知道这个功能很丑陋,但我要去看看我能兑现承诺的程度。

function pageThroughLikes(facebookPostArray) {
  var testArray = []
  return new Promise(function (fulfill, reject) {
    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{
                $.get(nextPage, 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')
                })
                i += 1;
                } while (currentDataLength != 0 && i > 10)
              }
           }
         }
      })
   });
   fulfill();
   console.log('paged through likes')
 })
}

此功能完成后,我想运行一个'测试'功能,将生成的数组转换为CSV格式并下载CSV文件。

这是我的测试功能:

function test() {
  var convertedPostCSV = convertArrayOfObjectsToCSV(postArray);
  downloadCSV(convertedPostCSV);
}

这是我的功能运行顺序:

$(document).ready(function() {
  getPostLikes().then(function() {
    pageThroughLikes(postArray).then(function() {
      test();
    });
  });
});

我正在努力的是我的'test()'函数在新的热门页面的数据被添加到我的'facebookPostArray'之前,或者在pageThroughLikes函数结束之前运行。

希望有人能给我一些建议/指出我正确的方向。

修改

好的,我已经重新格式化了我的代码,因为@Winter Soldier建议但我仍然收到processData函数未被捕获的错误。

这是我的代码:

function pageThroughLikes(facebookPostArray) {
 console.log('paging through likes')
 var testArray = []
 var promiseList = [];
 return new Promise(function (fulfill, reject) {
   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{
                   promiseList.push(
                     $.ajax({url : nextPage
                       }))
                       i += 1;
                     } while (currentDataLength != 0 && i > 10)
                }
            }
        }
   });
   return promiseList;
   console.log('paged through likes')
})

 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(pageThroughLikes(postArray), this).done(function() {

    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 2.0

这是我所有丑陋的完整代码。仅供参考。图中的问题可能是我隐藏在其余代码中的某些内容,因此可能值得向您展示...

var facebookKey = config.FACEBOOK_KEY;

// ASSIGN QUERY TO VARIABLE

var likePage = getQueryVariable("likePage");
var sinceDate = getQueryVariable("sinceDate");
var likeArray = [];
var postArray = [];


function getQueryVariable(variable) {
  var query = window.location.search.substring(1);
  var vars = query.split('&');
  for (var i = 0; i < vars.length; i++) {
    var pair = vars[i].split('=');
    if (pair[0] == variable) {
      return pair[1];
    }
  }
  alert("Query Variable " + variable + " not found");
}

console.log("Page Search: " + likePage);
console.log("Since: " + sinceDate)

// FIND DATA FOR FACEBOOK PAGE POSTS SINCE CHOSEN DATE 

$(document).ready(function() {
  getPostLikes().then(function() {
  $.when.apply(pageThroughLikes(postArray), this).done(function() {
    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);
}

function getPostLikes(response) {
  return new Promise(function (fulfill, reject) {
    $.get("https://graph.facebook.com/v2.8/"+ likePage + "?fields=access_token,posts.since(" + sinceDate + "){likes{id}}&access_token=" + facebookKey, function (facebookData) {

      var likePageId = facebookData.id;
      var testPostArray = [];
      if ('posts' in facebookData) {
        var nextPage = facebookData.posts.paging.next;
        var check = 0;

        postArray.push(facebookData.posts.data);

        var currentDataLength = " "
        var i = 0
        if ('paging' in facebookData.posts) {
          console.log("new page available");
          do {
            $.ajax({
              async: false,
              type: "GET",
              url: nextPage,
              success: function(nextPageData) {
                console.log("New Page Accessed: " + nextPage)
                i++;
                console.log("Page Number: " + i)
                testPostArray.push(nextPageData.data);
                if ('paging' in nextPageData) {
                  nextPage = nextPageData.paging.next;
                  console.log("next page assigned");
                }
                currentDataLength = nextPageData.data.length;
                console.log(currentDataLength);
              }
            });
            console.log("DATA LENGTH: " + currentDataLength);
          } while (currentDataLength > 0);
          testPostArray.forEach(function(element) {
            console.log(element)
            postArray.push(element);
            fulfill();
          });
        }
      } else {
        console.log('Error: No facebook posts since this date!')
        reject();
      }
      console.log(postArray)
    });
  })
};
console.log("Downloading...")


function pageThroughLikes(facebookPostArray) {
 console.log('paging through likes')
 var testArray = []
 var promiseList = [];
   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{
                   promiseList.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;
}


  // AUTO DOWNLOAD CSV FILE

  function downloadCSV(args) {
    var data, filename, link;
    var csv = convertArrayOfObjectsToCSV(postArray);

    if (csv == null) return;

    filename = args.filename || 'export.csv';

    if (!csv.match(/^data:text\/csv/i)) {
      csv = 'data:text/csv;charset=utf-8,' + csv;
    }
    data = encodeURI(csv);

    link = document.createElement('a');
    link.setAttribute('href', data);
    link.setAttribute('download', filename);
    link.click();
  }
  // CONVERT FACEBOOK POSTS OBJECTS TO CSV FORMAT

  function convertArrayOfObjectsToCSV(args) {
    var result, ctr, keys, columnDelimiter, lineDelimiter, data;

    data = args || null;
    if (data == null || !data.length) {
      return null;
    }

    columnDelimiter = args.columnDelimiter || ',';
    lineDelimiter = args.lineDelimiter || '\n';

    keys = Object.keys(data[0]);

    result = '';
    result += "user_id" + columnDelimiter + " post_id" + columnDelimiter + " page_id";
    result += lineDelimiter;

    data.forEach(function(item) {
      item.forEach(function(post) {
        if ('likes' in post) {
          var likeArray = post.likes
          likeArray.data.forEach(function(like) {
            result += like.id + columnDelimiter + post.id.split('_').reverse() + lineDelimiter;
          });
        } else {
          result += columnDelimiter + post.id.split('_').reverse() + lineDelimiter;
        };
      });
    });
    console.log('converted to CSV')
  return result;
  }

编辑3.0

功能几乎是固定的,唯一的问题是它不循环。尽管运行完美!

function pageThroughLikes(facebookPostArray) {
 console.log('paging through likes')
 var testArray = []
 var promiseList = [];
 // return new Promise(function (fulfill, reject) {
   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{
                   promiseList.push(
                     $.ajax({url : nextPage
                       }).then(function(data, b, promise){
                       data.id = currentPostId;
                       if ('paging' in data) {
                         if ('next' in data.paging) {
                           nextPage = data.paging.next;
                         }
                       }
                       console.log(nextPage)
                       return promise;
                       }))
                       i += 1;
                       console.log(i)
                     } while (currentDataLength != 0 && i > 10)
              }
             }
           }
         })
   });
   console.log('paged through likes')
   return promiseList;
}

修改4.0

这是我目前的代码......

几乎所有的东西终于正常工作,设法从大量的深夜谷歌搜索中获取大量信息。

var facebookKey = config.FACEBOOK_KEY;

// ASSIGN QUERY TO VARIABLE
var likePage = getQueryVariable("likePage");
var sinceDate = getQueryVariable("sinceDate");
var likeArray = [];
var postArray = [];

function getQueryVariable(variable) {
  var query = window.location.search.substring(1);
  var vars = query.split('&');
  for (var i = 0; i < vars.length; i++) {
    var pair = vars[i].split('=');
    if (pair[0] == variable) {
      return pair[1];
    }
  }
  alert("Query Variable " + variable + " not found");
}
console.log("Page Search: " + likePage);
console.log("Since: " + sinceDate)

// FIND DATA FOR DOJOAPP FACEBOOK PAGE POSTS SINCE CHOSEN DATE

$(document).ready(function() {
  getPostLikes().then(function() {
    // console.log(postArray);
  pageThroughLikes(postArray, test)
 });
});

function test(postArray) {
  var convertedPostCSV = convertArrayOfObjectsToCSV(postArray);
  downloadCSV(convertedPostCSV);
}

function getPostLikes(response) {
  return new Promise(function (fulfill, reject) {
    $.get("https://graph.facebook.com/v2.8/"+ likePage + "?fields=access_token,posts.since(" + sinceDate + "){likes{id}}&access_token=" + facebookKey, function (facebookData) {
      var likePageId = facebookData.id;
      var testPostArray = [];
      if ('posts' in facebookData) {
        var nextPage = facebookData.posts.paging.next;
        var check = 0;
        postArray.push(facebookData.posts.data);
        var currentDataLength = " "
        var i = 0
        if ('paging' in facebookData.posts) {
          console.log("new page available");
          do {
            $.ajax({
              async: false,
              type: "GET",
              url: nextPage,
              success: function(nextPageData) {
                console.log("New Post Page Accessed: " + nextPage)
                i++;
                console.log("Paging Through Posts: " + i)
                testPostArray.push(nextPageData.data);
                if ('paging' in nextPageData) {
                  nextPage = nextPageData.paging.next;
                  console.log("next page assigned: " + nextPage);
                }
                currentDataLength = nextPageData.data.length;
                console.log(currentDataLength);
              }
            });
            console.log("DATA LENGTH: " + currentDataLength);
          } while (currentDataLength > 0);
          testPostArray.forEach(function(element) {
            // console.log(element)
            postArray.push(element);
            fulfill();
          });
        }
      } else {
        console.log('Error: No facebook posts since this date!')
        reject();
      }
      // console.log(postArray)
    });
  })
};
console.log("Downloading...")


function pageThroughLikes(facebookPostArray, callback) {
 console.log('paging through likes')
 var testArray = []
 var promiseList = [];
   facebookPostArray.forEach(function(array) {
     array.forEach(function(innerObject) {
       if ('likes' in innerObject && 'paging' in innerObject.likes && 'next' in innerObject.likes.paging) {
       var nextPage = innerObject.likes.paging.next;
       console.log('new likes page assigned: ' + nextPage);
       var currentPostId = innerObject.id;
       var noMorePages = false;
       var i = 0;
       do{
         $.ajax({
           url: nextPage,
           success: function(nextLikePageData) {
             createLikeObject(nextLikePageData, currentPostId, checkForPagesOfLikes, nextLikePageData, noMorePages)
             if ('paging' in nextLikePageData && 'next' in nextLikePageData.paging) {
               nextPage = nextLikePageData.paging.next;
             }
            }
          })
         i += 1
         console.log(i)
        } while (noMorePages = false);
       }
     })
   });
   console.log('paged through likes')
   callback();
}

function createLikeObject(likeData, postId, callback, args, fail) {
  likeArrayFormat = [];
  likeObject = {};
  likeObject.likes = {};
  likeObject.id = postId;
  likeObject.likes.data = []
  likeData.data.forEach(function(like) {
    likeObject.likes.data.push(like);
  });
  likeArrayFormat.push(likeObject);
  postArray.push(likeArrayFormat);
  console.log('pushed new like data to postArray')
  callback(args, fail)
}

function pushToArray(item, array, callback) {
  array.push(item);
  callback()
}

function checkForPagesOfLikes(data, noMorePages) {
  if ('paging' in data && 'next' in data.paging) {
      return true;
      console.log('NEW PAGE FOUND')
    }
  else {
    noMorePages = true;
    console.log('NO MORE PAGES OF LIKES FOR CURRENT OBJECT')
  }
}

  // AUTO DOWNLOAD CSV FILE
  function downloadCSV(args) {
    var data, filename, link;
    var csv = convertArrayOfObjectsToCSV(postArray);
    if (csv == null) return;
    filename = 'export.csv';
    if (!csv.match(/^data:text\/csv/i)) {
      csv = 'data:text/csv;charset=utf-8,' + csv;
    }
    data = encodeURI(csv);
    link = document.createElement('a');
    link.setAttribute('href', data);
    link.setAttribute('download', filename);
    link.click();
  }

  // CONVERT FACEBOOK POSTS OBJECTS TO CSV FORMAT

  function convertArrayOfObjectsToCSV(args, callback) {
    var result, ctr, keys, columnDelimiter, lineDelimiter, data;
    // console.log(args)
    data = args || null;
    if (data == null || !data.length) {
      return null;
    }
    columnDelimiter = args.columnDelimiter || ',';
    lineDelimiter = args.lineDelimiter || '\n';
    keys = Object.keys(data[0]);
    result = '';
    result += "user_id" + columnDelimiter + " post_id" + columnDelimiter + " page_id";
    result += lineDelimiter;
    // console.log(args)
    args.forEach(function(object) {
      // console.log(object)
      // console.log(object.length)
      if (object.length != 0) {
        object.forEach(function(item) {
          if ('likes' in item && 'data' in item.likes) {
            var postId = item.id;
            item.likes.data.forEach(function(likeId) {
              if ('id' in likeId) {
                // console.log(likeId)
                var likeArray = likeId;
                // console.log(likeArray)
                  result += likeArray.id + columnDelimiter + postId.split('_').reverse() + lineDelimiter;
              } else {
                result += columnDelimiter + postId.split('_').reverse() + lineDelimiter;
              };
            });
          }
        });
      }
    })
    console.log('converted to CSV')
  return result;
  callback();
  }

目前很大的问题是convertArrayObObjectsToCSVcreateLikeObject功能完成之前正在运行。思想回调会起作用,但似乎我没有做得对。

2 个答案:

答案 0 :(得分:0)

这可能是一种方法,因为你正在使用get调用,我已经修改为根本不使用promise,因为无论如何ajax都会返回promise。

这是一个伪代码,您可能需要调整一下才能使其运行

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);
}

编辑:

  • 参数是jquery $ .when返回的默认对象 功能
  • 这是一个子数组的集合,每个子数组由数据组成, 现状与承诺对象。
  • 我假设您需要整合所有get中的数据 调用testArray,这就是在什么时候做的 调用

答案 1 :(得分:-2)

您需要完整/解决承诺所需的价值。使用您需要的值调用函数履行。例如,如果要解析数组:

$.ajax({
url: "mySite",
type: "GET",
headers: { "ACCEPT": "application/json;odata=verbose" },
success: function(data){
    $.each(data.d.results, function(index) { 
    var $this = $(this);
    var courseName = $this.attr('Title'); 
    var courseNumber = $this.attr('Course_x0020_Number'); 
    var courseUrl = $this.attr('URL'); 
    var trainingGroup = $this.attr('Training_x0020_Group'); 
    var recurrence = $this.attr('Recurrence'); 

    var dynCourseId = courseName.replace(/\s+/g, '')

        if (trainingGroup == 'Group1') {
            if (recurrence == "Don't Specify") {recurrence = '';
            } else recurrence = " ("+recurrence+")"; 
            document.getElementById('officeListSpan').innerHTML += '<ul class="courseLists"><li><input type="checkbox" id="'+dynCourseId+'"/>'+courseName+recurrence+'</li></ul>';
        }
        if (trainingGroup == 'Group2') {
            if (recurrence == "Don't Specify") {recurrence = '';
            } else recurrence = " ("+recurrence+")"; 
            document.getElementById('labListSpan').innerHTML += '<ul class="courseLists"><li><input type="checkbox" id="'+dynCourseId+'"/>'+courseName+recurrence+'</li></ul>';
        }
    });
    },
    error:  function(){
    alert("Failed to query SharePoint list data. Please refresh (F5).");
    }
});
function prepareTrainings() {
var idSelector = function() { 
    return this.id; 
};
var checkedCourses = $(":checkbox:checked").map(idSelector).get();
    alert("IDs for Courses Selected: " + checkedCourses);