简体   繁体   中英

Chain angularjs loops with promises

I'm building my first ionic/angularjs. My app consists of several sequential stages, each has a DB query in search of a pattern. So each stage is basically a while loop that breaks when the pattern is found at the db. I have a whileloop with promises following this post . I have a problem with chaining while loops.

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

A single while loop works as expected, returns 11 txt entries from the async DB. The loop condition in this example is simply the loop count, I removed the real pattern code for simplicity. The logged console is: L1:0, L1:1, .., L1:12, done1. That's just fine.

The problem shows when I chain loops:

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

The unexpected output is:

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

Why are L2 and L3 running in parallel?

One problem is that your outer then s doesn't return any promises.

I believed until now that you would get unresolved promises, but that can't be true since you observe tasks running in parallel.

Another problem is that it's very hard to see what you are trying to do, when you are mixing both nested and chained promises. Why do you need a promiseWhile in the first place?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM