Skip to content

How to retrieve all posts of a user via Facebook Graph API using promises and recursion?

I am currently developing a web app which uses the Facebook Graph API.

What I would like to achieve is to get all posts of a user.

However, this is not that easy since I have to paginate the results.

At the moment I am struggeling with promises.

What I try to achieve is to fill an array with the post objects.

Therefore I use promises and recursion which does not work as expected.

My code currently looks as follows:

// Here I retrieve the user with his or her posts,
// just the first 25 due to pagination
if (accessToken) {
  return new Promise(resolve => {
    FB.api('/me?fields=id,name,posts&access_token=' + accessToken, response => {
      this.get('currentUser').set('content', response);

// Returns all posts of a given user
function getAllPostsOfUser(posts, postsArr) {
  // Process each post of the current pagination level
  for (var post of {
    // Only store meaningful posts
    if (post !== undefined && post.message !== undefined) {

  // Further posts are retrievalable via which is an url
  if ( !== 0 && !== undefined) {
    FB.api(, response => {
      getAllPostsOfUser(response, postsArr)

  return postsArr

var posts = getAllPostsOfUser(this.get('currentUser').content.posts, [])
// I want to use all the posts here

The problem I have is that I want to use the posts where the console.log is placed but when I log the posts array a lot of posts are missing.

I am sure that I did something wrong with the promises but I do not know what.

I would be glad if anyone could guide me to a solution.

Thank you in advance.



Try this:

function getAllPosts() {
  return new Promise((resolve, reject) => {
    let postsArr = [];
    function recursiveAPICall(apiURL) {
      FB.api(apiURL, (response) => {
        if (response && {
          //add response to posts array (merge arrays), check if there is more data via paging
          postsArr = postsArr.concat(;
          if (response.paging && {
          } else {
        } else {

  .then((response) => {
  .catch((e) => {

Not tested, just a quick example I came up with. It returns a promise and uses a recursive function to get all entries. BTW, you don’t need to add the Access Token. If you are logged in, the SDK will use it internally.