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