Chain angularjs 循环与承诺
Chain angularjs loops with promises
我正在构建我的第一个 ionic/angularjs。我的应用程序由几个连续的阶段组成,每个阶段都有一个数据库查询来搜索模式。因此,每个阶段基本上都是一个 while 循环,当在数据库中找到模式时,该循环就会中断。我有一个 whileloop,其中包含 this post 之后的承诺。我在链接 while 循环时遇到问题。
Utils.promiseWhile = function(condition, body) {
var done = $q.defer(); //also tried: new $q.defer();
function loop() {
if (!condition()) return done.resolve();
$q.when(body(), loop, done.reject);
}
setTimeout(loop, 1);
return done.promise;
}
单个 while 循环按预期工作,returns 来自异步数据库的 11 个 txt 条目。本例中的循环条件只是循环计数,为简单起见,我删除了真正的模式代码。记录的控制台是:L1:0、L1:1、..、L1:12、done1。这样就好了。
当我链接循环时出现问题:
var idx1 = 0;
var idx2 = 0;
var idx3 = 0;
var txt ='';
Utils.promiseWhile(
function () { console.log("L1: "+idx1); return idx1 <= 11;},
function () {
idx1++;
return MYDB.txt(idx1,1)
.then(function(t){txt += (' '+t);});
})
.then(function () {
console.log("done1");
Utils.promiseWhile(
function () { console.log("L2: "+idx2); return idx2 <= 11;},
function () {
idx2++;
return MYDB.txt(idx2,1)
.then(function(t){txt += (' '+t);});
})
})
.then(function () {
console.log("done2");
Utils.promiseWhile(
function () { console.log("L3: "+idx3); return idx3 <= 11;},
function () {
idx3++;
return MYDB.txt(idx3,1)
.then(function(t){txt += (' '+t);});
})
})
.then(function(){
console.log("done3");
});
意外的输出是:
L1: 0
L1: 1
L1: 2
L1: 3
L1: 4
L1: 5
L1: 6
L1: 7
L1: 8
L1: 9
L1: 10
L1: 11
L1: 12
done1
done2
done3
L2: 0
L3: 0
L2: 1
L3: 1
L2: 2
L3: 2
L2: 3
L3: 3
L2: 4
L3: 4
L2: 5
L3: 5
L2: 6
L3: 6
L2: 7
L3: 7
L2: 8
L3: 8
L2: 9
L3: 9
L2: 10
L3: 10
L2: 11
L3: 11
L2: 12
L3: 12
为什么 L2 和 L3 运行 并联?
一个问题是你的外层 then
没有 return 任何承诺。
直到现在我都相信您会得到未解决的承诺,但事实并非如此,因为您同时观察任务 运行。
另一个问题是,当您混合使用嵌套和链式 promise 时,很难看出您正在尝试做什么。为什么首先需要 promiseWhile
?
我正在构建我的第一个 ionic/angularjs。我的应用程序由几个连续的阶段组成,每个阶段都有一个数据库查询来搜索模式。因此,每个阶段基本上都是一个 while 循环,当在数据库中找到模式时,该循环就会中断。我有一个 whileloop,其中包含 this post 之后的承诺。我在链接 while 循环时遇到问题。
Utils.promiseWhile = function(condition, body) {
var done = $q.defer(); //also tried: new $q.defer();
function loop() {
if (!condition()) return done.resolve();
$q.when(body(), loop, done.reject);
}
setTimeout(loop, 1);
return done.promise;
}
单个 while 循环按预期工作,returns 来自异步数据库的 11 个 txt 条目。本例中的循环条件只是循环计数,为简单起见,我删除了真正的模式代码。记录的控制台是:L1:0、L1:1、..、L1:12、done1。这样就好了。
当我链接循环时出现问题:
var idx1 = 0;
var idx2 = 0;
var idx3 = 0;
var txt ='';
Utils.promiseWhile(
function () { console.log("L1: "+idx1); return idx1 <= 11;},
function () {
idx1++;
return MYDB.txt(idx1,1)
.then(function(t){txt += (' '+t);});
})
.then(function () {
console.log("done1");
Utils.promiseWhile(
function () { console.log("L2: "+idx2); return idx2 <= 11;},
function () {
idx2++;
return MYDB.txt(idx2,1)
.then(function(t){txt += (' '+t);});
})
})
.then(function () {
console.log("done2");
Utils.promiseWhile(
function () { console.log("L3: "+idx3); return idx3 <= 11;},
function () {
idx3++;
return MYDB.txt(idx3,1)
.then(function(t){txt += (' '+t);});
})
})
.then(function(){
console.log("done3");
});
意外的输出是:
L1: 0
L1: 1
L1: 2
L1: 3
L1: 4
L1: 5
L1: 6
L1: 7
L1: 8
L1: 9
L1: 10
L1: 11
L1: 12
done1
done2
done3
L2: 0
L3: 0
L2: 1
L3: 1
L2: 2
L3: 2
L2: 3
L3: 3
L2: 4
L3: 4
L2: 5
L3: 5
L2: 6
L3: 6
L2: 7
L3: 7
L2: 8
L3: 8
L2: 9
L3: 9
L2: 10
L3: 10
L2: 11
L3: 11
L2: 12
L3: 12
为什么 L2 和 L3 运行 并联?
一个问题是你的外层 then
没有 return 任何承诺。
直到现在我都相信您会得到未解决的承诺,但事实并非如此,因为您同时观察任务 运行。
另一个问题是,当您混合使用嵌套和链式 promise 时,很难看出您正在尝试做什么。为什么首先需要 promiseWhile
?