Skip to content

Instantly share code, notes, and snippets.

@mattvv
Created September 4, 2018 17:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattvv/375e45a623661d37f3e6062f15142ae8 to your computer and use it in GitHub Desktop.
Save mattvv/375e45a623661d37f3e6062f15142ae8 to your computer and use it in GitHub Desktop.
Facebook API only returns pages of friends that use the app. This code allows you to fetch all the users of the user who use the app and exposes it in an async function instead of the callback implementation
import { GraphRequest, GraphRequestManager } from 'react-native-fbsdk';
//Gets all your friends who have used the app, exploding facebook's caching.
export const getFriends = async () => {
try {
let { pagedFriends, cursor } = await friendsRequest();
let allFriends = pagedFriends;
while (pagedFriends.length > 0) {
let request = await friendsRequest(cursor);
pagedFriends = request.pagedFriends;
cursor = request.cursor;
allFriends = [...allFriends, ...pagedFriends];
}
return allFriends;
} catch (error) {
return [];
}
};
const friendsRequest = startFrom => {
const requestUrl = !startFrom ? `/me/friends` : `/me/friends?after=${startFrom}`;
return new Promise((resolve, reject) => {
const friendsRequest = new GraphRequest(requestUrl, null, (error, result) => {
if (error) {
reject(error);
} else {
const cursors = result.data.length > 0 ? result.paging.cursors.after : undefined;
resolve({ pagedFriends: result.data, cursor: cursors });
}
});
new GraphRequestManager().addRequest(friendsRequest).start();
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment