嵌套 context.executeQueryAsync 与延迟
Nested context.executeQueryAsync with Deferred
如何将嵌套 context.executeQueryAsync 与 Deferred 一起使用?下面是我的代码,我将解释我到底在寻找什么:
代码
function getValues() {
var dfd = $.Deferred(function () {
context.executeQueryAsync(function () {
var navigationItem = [];
// First Loop
while (termEnumerator.moveNext()) {
// Push Parent Terms in navigationItem array
navigationItem.push({ "name": ""});
// Get Sub Terms
context.executeQueryAsync(function () {
// Second Loop
while (termsEnum.moveNext()) {
// Push Sub Terms in navigationItem array
navigationItem.push({ "name": ""});
}
}, function (sender, args) {
console.log(args.get_message());
});
}
dfd.resolve(navigationItem);
}, function (sender, args) {
console.log(args.get_message());
dfd.reject(args.get_message());
});
});
return dfd.promise();
}
基本上,我正在尝试使用上述代码结构在 SharePoint Online 中获取分类法(术语及其子术语)。最初我创建了一个名为 navigationItem
的数组并遍历所有术语。
在迭代期间,首先,我将术语推入此数组,与此同时,我还获取它的子术语(如果有)并将其推入同一数组。
我希望代码在第二个循环完成之前不会继续执行。这样我在将它返回到另一个函数时将拥有最终数组。
I want that code doesn't execute further until second loop completes
it's execution. So that I will have final array while returning it to
another function.
在这种情况下,您需要为每个 executeQueryAsync 延迟。
然后,您需要创建一个整体延迟以等待所有异步方法完成。
下面是供您参考的示例代码:
(function ($) {
function executeQueryAsync(succeededCallback, failedCallback)
{
var period = Math.random() * 10000;
setTimeout(function () {
succeededCallback();
}, period);
}
function forEachAsync(items, funcAsync, callback)
{
var count = 0;
var total = $.Deferred();
function increment()
{
count++;
if(count == items.length)
{
total.resolve();
}
}
for (var i = 0; i < items.length; i++)
{
(function exec(item) {
var deferred = $.Deferred(function (defer) {
funcAsync(function () {
callback();
defer.resolve();
});
});
deferred.done(function () {
increment();
});
})(items[i]);
}
return total.promise();
}
var promise = forEachAsync([1, 2, 3, 4, 5], executeQueryAsync, function () {
console.log('call back executing + ' + Date.now());
});
promise.done(function () {
console.log("promise done");
});
})(jQuery);
如何将嵌套 context.executeQueryAsync 与 Deferred 一起使用?下面是我的代码,我将解释我到底在寻找什么:
代码
function getValues() {
var dfd = $.Deferred(function () {
context.executeQueryAsync(function () {
var navigationItem = [];
// First Loop
while (termEnumerator.moveNext()) {
// Push Parent Terms in navigationItem array
navigationItem.push({ "name": ""});
// Get Sub Terms
context.executeQueryAsync(function () {
// Second Loop
while (termsEnum.moveNext()) {
// Push Sub Terms in navigationItem array
navigationItem.push({ "name": ""});
}
}, function (sender, args) {
console.log(args.get_message());
});
}
dfd.resolve(navigationItem);
}, function (sender, args) {
console.log(args.get_message());
dfd.reject(args.get_message());
});
});
return dfd.promise();
}
基本上,我正在尝试使用上述代码结构在 SharePoint Online 中获取分类法(术语及其子术语)。最初我创建了一个名为 navigationItem
的数组并遍历所有术语。
在迭代期间,首先,我将术语推入此数组,与此同时,我还获取它的子术语(如果有)并将其推入同一数组。
我希望代码在第二个循环完成之前不会继续执行。这样我在将它返回到另一个函数时将拥有最终数组。
I want that code doesn't execute further until second loop completes it's execution. So that I will have final array while returning it to another function.
在这种情况下,您需要为每个 executeQueryAsync 延迟。
然后,您需要创建一个整体延迟以等待所有异步方法完成。
下面是供您参考的示例代码:
(function ($) {
function executeQueryAsync(succeededCallback, failedCallback)
{
var period = Math.random() * 10000;
setTimeout(function () {
succeededCallback();
}, period);
}
function forEachAsync(items, funcAsync, callback)
{
var count = 0;
var total = $.Deferred();
function increment()
{
count++;
if(count == items.length)
{
total.resolve();
}
}
for (var i = 0; i < items.length; i++)
{
(function exec(item) {
var deferred = $.Deferred(function (defer) {
funcAsync(function () {
callback();
defer.resolve();
});
});
deferred.done(function () {
increment();
});
})(items[i]);
}
return total.promise();
}
var promise = forEachAsync([1, 2, 3, 4, 5], executeQueryAsync, function () {
console.log('call back executing + ' + Date.now());
});
promise.done(function () {
console.log("promise done");
});
})(jQuery);