如何在异步方法上成功执行回调

时间:2019-05-09 14:51:34

标签: javascript callback

我目前正试图从包含异步函数的函数中返回一个值,但我被卡住了。

我当前正在使用SharePoint 2013 JSOM从“人员”列中提取电子邮件地址。我找到了一个很好的函数来做到这一点,我选择将userID传递给函数。

该函数本身包含console.log并输出预期的结果,但是,我需要这些结果,因为我首先从那里调用了该函数,所以我发现我需要使用回调。我无法从调用方法中提取变量。

    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);
    }

我想要的q是等于电子邮件地址,这样我就可以在调用函数的其他地方使用它。

无论我尝试什么,我都会得到“未定义”。

我可以将console.log放置在function(returnedValue){}中),但这仍然不能让我了解变量。没有足够的JScript来理解提议的副本上非常复杂的问题。

1 个答案:

答案 0 :(得分:0)

function getEUEmail(userId, JT,  flag, callback) {

    var x = [];

    var contextEU = new SP.ClientContext();

    var web = contextEU.get_web();

    var user = web.get_siteUsers().getById(userId);

    contextEU.load(user);

    contextEU.executeQueryAsync(function() {

        //console.log(user.get_email());

        var y =   "<div class='tablewrapper'>" +
      "<div class='table'>" +
        "<div class='row'>" +
          "<div class='rowspanned cell'>" +
             ' <img class="contacts" src="' + _spPageContextInfo.webServerRelativeUrl + '/_layouts/15/userphoto.aspx?size=M&accountname='+ user.get_email()  +'"/>' +
          "</div>" +
          "<div class='cell'>" +
           user.get_title() +
          "</div>" +
        "</div>" +
        "<div class='row'>" +
          "<div class='empty cell'></div>" +
          "<div class='cell'>" +
            JT +  
          "<div class='cell'>" +
         ' <img class="flag" src="' + window.location.protocol + "//" + window.location.host   + '/SiteAssets/Images/'+ flag  +'.png"/>' +
         //http://staging.intranet.ent.sys.element.com/SiteAssets/Images/EU.png
          "</div>" +
        "</div>" +
        "</div>" +
      "</div>" +
    "</div>"

        x.push(y);

        callback(x);
}

然后使用它

       getEUEmail(t,u, v, function(returnedValueEU) {

            //console.log(returnedValue[0])

            $("#divListItemsEU").append(

            "<style type='text/css'> .tablewrapper { position: relative;  box-sizing: border-box; height:72px} .table {display: table; } .row { display: table-row; padding: 1px; } .cell { display: table-cell; border: none solid red; padding: 3px;} .cell.empty { border: none; width: 75px; } .cell.rowspanned {  position: absolute; top: 0;  bottom: 0; width: 75px; display: block; margin: 1px;} .contacts{ width: 72px; height: 72px;} .flag { width: 30px; height: 20px; }</style> " +

                    "" + returnedValueEU[0]  + 




                    '<br />');
});

    $("#divListItemsEU").html(listEUItemInfoEU);

}

我最终将所有值传递给getEmail函数,构建HTML,然后研究了如何在一些帮助下利用回调。可能不是正确的,有效的或优雅的,但确实可行。谢谢