Returning A Variable From The Callback Of A Function
Solution 1:
I assume the Facebook API is asynchronous. So at the time return result
is executed, the callback was not called yet. You have to provide a callback from the calling function:
functioncheck_perm(perm, callback) {
var result;
FB.api(
{
method: 'users.hasAppPermission',
ext_perm: perm
}, callback);
}
and
functionloop_perms(permissions) {
check_perm(whatever here, function(result) {
// do something with result
});
}
Solution 2:
It looks like you're calling FB.api
asychronously, passing it an anonymous function as a callback that gives result
a value. check_perm
is returning before the callback is executed, at which point result
is still undefined.
Solution 3:
Problem 1: result is readable by the callback, but it is a copy so not writeable in the way you're thinking. (writeable locally not globally, this sub-problem could be "fixed" by making it a global variable, but it still wouldn't make your code work)
Problem 2: FB.api most likely calls that callback asynchronously in the future letting check_perm return immediately with result in whatever state it currently is in (undefined)
You need to put the code that deals with the actual response inside that callback function to be executed when then response is available. You wouldn't even need the result variable at that point.
Post a Comment for "Returning A Variable From The Callback Of A Function"