I have an array of facebook posts that I have each processed into a structured format, such as:
var refined_posts = [];
var refined_post = {
info1: info1,
info2: info2,
username: username,
profilelink: profilelink
}
info1 and info2 is returned from my first Async Facebook Graphs API call, which grabs a facebook post:
// Get the facebook post
FB.api(
"/142679255268",
function (response) {
if (response && !response.error) {
info1: response.info1,
info2: response.info2,
postid: response.id
}
}
);
And after than I have 2 other Async calls for extracting the user name of a facebook post, and the profile link URL:
// Get the name of the poster
FB.api(
'/'+postid+'?fields=from',
function (response) {
if (response && !response.error) {
username = response.from.name;
}
}
);
// Get the profile link of the user
FB.api(
'/'+username+'?fields=link',
function (response) {
if (response && !response.error) {
profilelink= response.link;
}
}
);
And then finally, I push the refined_post to the array.
refined_posts.push(refined_post);
However, currently the refined_post will get pushed to the array before the last 2 async calls complete. How can I make the push wait for all Facebook async calls to finish before pushing?
I am using AngularJS but I would also like to be able to reuse my frontend code in my NodeJS backend. For now my priority is getting this to work on the frontend. Any ideas?