我想学习如何从一个函数返回值,然后在另一个函数中使用它。我可以在div中显示该值,也可以在警报窗口中获取它,但是我需要在其他函数中使用该值。
<script type="text/javascript">
SP.SOD.executeOrDelayUntilScriptLoaded(testCase,"sp.js");
function showUserInfo(){
var clientContext = SP.ClientContext.get_current();
var user = clientContext.get_web().get_currentUser();
clientContext.load(user);
clientContext.executeQueryAsync(function(){
return user.get_loginName();
},function(sender,args){alert(args.get_message());})
}
function testCase() {
var test = showUserInfo();
alert(test);
}
</script>
如果您需要支持Internet Explorer
要支持Internet Explorer,应使用类似于@Amos_MSFT建议的回调函数。在下面,您将找到我的解决方案,该解决方案与@Amos_MSFW发布的解决方案非常相似,但有一些区别和注释。
// Execute the testCase() function only after sp.js has loaded and is ready.
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function() {
textCase();
});
function testCase() {
getLoginName(function(error, success) {
if (error) {
alert(error);
} else {
/*
* The line below is not really necessary, as the result of the
* getLoginName() function is already available in the success
* variable. You can already pass it to other functions, like
* this: alert(success);
*/
var loginName = success;
alert(loginName);
}
});
}
function getLoginName(callback) {
var clientContext = SP.ClientContext.get_current();
var user = clientContext.get_web().get_currentUser();
clientContext.load(user);
clientContext.executeQueryAsync(
function() {
/*
* The first argument of the callback() function is used for the
* error message, so we need to use null as the first argument
* when we want to return the login name as the success message.
*/
callback(null, user.get_loginName());
},
function(_sender, args) {
callback(args.get_message());
}
);
}
如果不需要支持Internet Explorer,则
我建议您在不需要支持Internet Explorer的情况下使用Promise。 Promise是一种特殊的对象,代表尚未完成的操作,它们使异步操作的工作变得轻松而有趣。我不够流利,无法详细解释它们,因此,如果您有兴趣,建议您阅读上面链接的文章。不过,我将告诉您的是,我们可以使用Promise来确保testCase()
功能停止并等待showUserInfo()
功能完成,就像在等待登录名可用一样。
我还没有机会测试下面的代码,但是应该可以。如果没有,请告诉我。如果您对Promises还不熟悉,我还添加了一些评论。
const showUserInfo = () => {
return new Promise((resolve, reject) => {
const clientContext = SP.ClientContext.get_current();
const currentUser = clientContext.get_web().get_currentUser();
clientContext.load(currentUser);
clientContext.executeQueryAsync(
() => {
/*
* We resolve the Promise when the query has executed successfully.
* Resolving a Promise means marking it as fullfilled, or complete, and
* returning the current user's login name.
*/
resolve(currentUser.get_loginName());
},
(_sender, args) => {
/*
* If something goes wrong, we reject the Promise. Rejecting means
* marking it as failed, while still returning something. In this
* case, we return the error message.
*/
reject(args.get_message());
}
);
});
}
const testCase = async () => {
/*
* We use the await keyword to halt the code and wait for the showUserInfo()
* function to finish. What really happens is that we wait for the Promise
* in the showUserInfo() function to be marked as settled, whether it is
* fullfilled or rejected, and then assign the result to the test constant.
*/
const test = await showUserInfo();
alert(test);
}
SP.SOD.executeOrDelayUntilScriptLoaded(testCase, 'sp.js');
函数executeQueryAsync是异步的。您可以使用回调函数获取返回值。示例代码供您参考:
<script type="text/javascript">
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function(){
var testCase=function(callback){
var clientContext = SP.ClientContext.get_current();
var user = clientContext.get_web().get_currentUser();
clientContext.load(user);
clientContext.executeQueryAsync(function(){
callback(null,user.get_loginName()) ;
},function(sender,args){alert(args.get_message());})
}
testCase(function(error,name){
if (error) {
console.error(error);
return;
}
console.log(name);
})
});
</script>