I am experimenting with the facebook API to retrieve information about events.
I made functions to retrieve the event's name, date, location, ... But i'm struggling to return the event information from it's function correctly.
function retrieveEventName(eventID) {
var name = "test";
FB.api(eventID, // Make the API call
function (response) {
if (response && !response.error) {
name = response.name;
// name = correct name of the event (NOT "test" any more!)
}
}
);
// name = test "name" lost it's event name and is "test" again, WHY?
return name; // returns "test" instead of the event's name...
}
I must be missing something elementary about javascript, but I was not able to find what is causing this problem (I presume a problem with scopes (caused by the nested function in the API call) but could not find any information about JS scopes that would explain the problem).
Note that i am sure that name
is the name of the event when i'm in the if
clause (I printed it to test it).