Parse Help - Develephant?

I have a basic Parse setup that contains two classes: User and GameWorld. A User has a 1-1 relation with a GameWorld object.

Within the User object I store a Player’s Friend’s IDs as an array of Pointers (to their User Class), which I want to be able to access their GameWorld values with; the problem I’m facing is that I iterate through this array of pointers and fire off a query with the following code:

[lua]

function Player:getFriendsValues(callback)
    local function onGetUser( event )
        local friendData = {}
        if not event.error then
            print( event.response.objectId )
            for i = 1, #event.response[“Friends”] do
                table.insert(friendData, event.response[“Friends”][i])
                local query = { [“where”] = { [“player”] = { ["__type"] = “Pointer”, [“className”] = Parse.USER_CLASS, [“objectId”] = friendData[i].objectId } } }
                Parse:getObjects( “GameWorld”, query,
                function(e)
                    for x=1, #e.results do
                        local friendValue = e.results[x].Value
                        totalValue = totalValue + friendValue
                        if i == #event.response[“Friends”] then
                            callback(totalValue)
                        end
                    end
                end)
            end
        else
            callback(false, event)
        end
    end
    Parse:getMe(onGetUser)
end

[/lua]

Ultimately I’m trying to collate each of the individual Friend’s values and then hit a callback function with this value - the problem I’m facing is due to the asynchronous nature of the Parse queries - I can’t seem to be sure when I need to make the call to the callback as the nature of the queries and responses means this isn’t certain.

Any help/opinions/expertise would be greatly appreciated…