Skip to content

Instantly share code, notes, and snippets.

@mattvv
Created September 4, 2018 17:07
Show Gist options
  • Select an option

  • Save mattvv/375e45a623661d37f3e6062f15142ae8 to your computer and use it in GitHub Desktop.

Select an option

Save mattvv/375e45a623661d37f3e6062f15142ae8 to your computer and use it in GitHub Desktop.

Revisions

  1. Matt Van renamed this gist Sep 4, 2018. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. Matt Van created this gist Sep 4, 2018.
    34 changes: 34 additions & 0 deletions facebookToAsyncAwait
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    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();
    });
    };