0

Ok I know the answer to this is easy I just have very very little experience in Javascript. Inside ctx.ExecuteQuery the alert pops up the value I want. How do i get it down below to the userName.Split('|'); Total newbie here so please do not make fun of me to bad for not knowing what I am doing..

Edit: Can i return a var from the Async Function this is my Source https://msdn.microsoft.com/en-us/library/office/ee535262(v=office.14).aspx?cs-save-lang=1&cs-lang=javascript#code-snippet-2 I would like to return or if there is a better way

var user = ctx.get_web().get_currentUser();
        ctx.load(user);

        var userName;
        ctx.executeQueryAsync(function(){
                userName = user.get_loginName(); 
                alert(userName);
                }, function(){alert(":(");});



        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        var userSplit = userName.split('|');
user3753693
  • 225
  • 1
  • 5
  • 13
  • 1
    You are trying to eat the pizza before the delivery guy has brought it to your house. There is a reason why there is the callback function where the alert works. The call is asynchronou, it does not wait , it just keeps on going. http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call – epascarello Jan 22 '16 at 01:30
  • Can i return a Var from the Async Function like I would a normal Function? – user3753693 Jan 22 '16 at 01:59

2 Answers2

1

The answer is no.

You should use promise or callback.

emil
  • 6,074
  • 4
  • 30
  • 38
-1

You can cache off the userSplit before calling the async function. And set it in the async callback, right where your alert is.

    var user = ctx.get_web().get_currentUser();
    ctx.load(user);

    var userName;
    var userSplit;
    ctx.executeQueryAsync(function() {
      userName = user.get_loginName();
      alert(userName);
    }, function() {
      alert(":(");
      userSplit = userName.split('|');
    });
mariocatch
  • 8,305
  • 8
  • 50
  • 71