I am calling this function, assigning the result to a variable in the callback and then logging the result, but I keep getting undefined.
var id;
test.getID(function(result) {
id=result;
});
console.log(id);
If I change it to the code below, then I can see the id logged.
var id;
test.getID(function(result) {
id=result;
console.log(id);
});
Do you know what I can do to be able to access the result of the getID function?
The
getID
function will need to invoke its parameter before you will seeid
change.Since you do not provide its implementation, let's assume it's something like this. Pay close attention to the implementation of
getID
, it takes a function as the parameter,f
, and then invokes it. This is whenid
will be set.A closure would work for you as well: