var Q = require('q');

function findFBUser(api, search_str, allowNonFriends) {
    var userID;
    var promise;
    if (/^\d+$/.test(search_str)) {
        //if all numbers assume it is the userid
        userID = search_str;
        promise = Q.nfcall(api.getUserInfo, userID);
    } else {
        promise = getFBUserIDByName(api, search_str).then(id => {
            userID = id;
            return Q.nfcall(api.getUserInfo, id);
        });
    }
    promise = promise.then(userInfoMap => {
        var userInfo = userInfoMap[userID];
        if (!userInfo.isFriend && !allowNonFriends)
            throw new Error(
                'User not your friend, they may not be your top ' +
                    name +
                    ", try using '@facebot friends <partial_name>' to get their id or fb vanity name to use"
            );

        // The userinfo object doesnt have an id with it, so add it
        userInfo.id = userID;
        return userInfo;
    });
    return promise;
}

function getFBUserIDByName(api, name) {
    return Q.nfcall(api.getUserID, name).then(data => {
        return data[0].userID;
    });
}

module.exports = {
    findFBUser: findFBUser,
};

@@ -1,11 +1,13 @@
-var
+const
  Q = req
@@ -22,189 +22,264 @@
 );%0A%0A
-function findFBUser(api, search_str, allowNonFriends) %7B%0A    var
+// Regex to test if the string is likely a facebook
  user
+ 
 ID
-;%0A    var promise;%0A    if (/%5E%5Cd+$/.test(search_str)) %7B%0A        //if all numbers assume it is the userid%0A   
+%0Aconst USER_ID_REGEX = /%5E%5Cd+$/;%0A%0Aasync function getFBUserInfoByID(api, id) %7B%0A    return await Q.nfcall(api.getUserInfo, id);%0A%7D%0A%0Aasync function findFBUser(api, search_str, allowNonFriends) %7B%0A
     
+let
  use
@@ -300,137 +300,137 @@
 tr;%0A
+%0A
     
-    promise = Q.nfcall(api.getUserInfo, userID);%0A    %7D else %7B%0A        promise = getFBUserIDByName(api, search_str).then(id =%3E
+// If the search string isnt a userID, we should search%0A    // for the user by name%0A    if (!USER_ID_REGEX.test(search_str))
  %7B%0A 
@@ -440,43 +440,28 @@
     
-   
+let
  user
-ID = id;%0A            return
+Data = await
  Q.n
@@ -478,23 +478,23 @@
 getUserI
-nfo, id
+D, name
 );%0A     
@@ -500,18 +500,43 @@
     
-%7D)
+userID = userData%5B0%5D.userID
 ;%0A    %7D%0A
     
@@ -535,63 +535,80 @@
   %7D%0A
+%0A
     
-promise = promise.then(userInfoMap =%3E %7B%0A        var
+const userInfoMap = await getFBUserInfoByID(api, userID);%0A    const
  use
@@ -636,20 +636,17 @@
 serID%5D;%0A
-    
+%0A
     if (
@@ -693,20 +693,16 @@
         
-    
 throw ne
@@ -710,20 +710,16 @@
  Error(%0A
-    
         
@@ -790,20 +790,16 @@
         
-    
 name +%0A 
@@ -813,20 +813,16 @@
         
-    
 %22, try u
@@ -914,20 +914,12 @@
     
-    );%0A%0A    
+);%0A%0A
     
@@ -981,195 +981,48 @@
 d it
-%0A        userInfo.id = userID;%0A        return userInfo;%0A    %7D);%0A    return promise;%0A%7D%0A%0Afunction getFBUserIDByName(api, name) %7B%0A    return Q.nfcall(api.getUserID, name).then(data =%3E %7B%0A    
+ as its useful%0A    userID.id = userID;%0A%0A
     
@@ -1032,30 +1032,16 @@
 urn 
-data%5B0%5D.userID;%0A    %7D)
+userInfo
 ;%0A%7D%0A
@@ -1067,20 +1067,8 @@
 %0A   
- findFBUser:
  fin

Refactor Utils file to use Async functions #feelsgoodman