gpt4 book ai didi

javascript - 如何从 executeQueryAsync 获取返回值?

转载 作者:行者123 更新时间:2023-11-30 21:02:36 25 4
gpt4 key购买 nike

我不知道如何从下面的方法中获取返回值。我正在将项目添加到数组中。那很好用。我就是无法获取函数返回的数组。

var termList = loadTerms(termSetId);

function loadTerms(termSetId) {

var termList = [];
var taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(clientContext);
var termStore = taxSession.getDefaultSiteCollectionTermStore();
var termSet = termStore.getTermSet(termSetId);
var terms = termSet.getAllTerms();

clientContext.load(terms, 'Include(Name)');

clientContext.executeQueryAsync(
function () {

for (var i = 0; i < terms.get_count(); i++) {

var term = terms.getItemAtIndex(i);
termList.push(term);
console.log(String.format('12 Term : {0}', term.get_name()));
}

// At this point TermList has the values I need. How do I return it to the caller?

});
}

最佳答案

这是不可能的,因为您不能从同步方法中的异步调用返回。但是你可以通过传递一个回调函数来得到你想要的。

var termList = loadTerms(termSetId);

function loadTerms(termSetId, callback) {

var termList = [];
var taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(clientContext);
var termStore = taxSession.getDefaultSiteCollectionTermStore();
var termSet = termStore.getTermSet(termSetId);
var terms = termSet.getAllTerms();

clientContext.load(terms, 'Include(Name)');

clientContext.executeQueryAsync(
function() {

for (var i = 0; i < terms.get_count(); i++) {

var term = terms.getItemAtIndex(i);
termList.push(term);
console.log(String.format('12 Term : {0}', term.get_name()));
}

callback(termList); // callback here

});
}

loadTerms("termSetId", function(returnedValue) {
console.log(returnedValue); //You get the value here.
});

关于javascript - 如何从 executeQueryAsync 获取返回值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46985962/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com