Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.