I am currently trying to return a value from a function that contains an async function and I'm stuck.
I am currently working with SharePoint 2013 JSOM to extract the email address from a Person column. I have found a nice function that does this and I elected to pass the userID into the function.
The function itself contains console.log and outputs the expected result, however, I need those results up where I called the function from in the first place so I found that I need to use a callback. I cannot extract a variable from the calling method.
var t = oListItem.get_item('ElementContactFullName').get_lookupId();
var q = getEmail(t, function(returnedValue){});
function getEmail(userId, callback) {
var x = [];
var context = new SP.ClientContext();
var web = context.get_web();
var user = web.get_siteUsers().getById(userId);
context.load(user);
context.executeQueryAsync(function() {
//console.log(user.get_email());
var y = user.get_email();
x.push(y);
}
, function() {
console.log("error");
});
callback(x);
}
What I want is q to equal the email address so I can use it elsewhere in the calling function.
What I get is "Undefined" no matter what I try.
I can place console.log in function(returnedValue){}) but that still doesn't let me get at the variable. Not done enough JScript to understand the very complex dicussions on the proposed duplicate.