Identify when nested FOR loops and IF statement with one asynchronous function call are done


#1

I have a function with nested for-loops and IF statements and one call of an asynchronous function. The structure looks similar to this:

search(){
    // for loop 1
    for (let i in object){

      // IF statement 1
      if (condition1){

        // for loop 2
        for (let o in object[i]){

          // IF statement 2
          if (condition2){

            // call of an asynchronous function
            this.asyncFunction().then( data => {

              this.result.push(data)

            });
          }
        }
      }
    }
  }

I want to call one function (let’s say goToNextPage() ) as soon as all loops are done and all async-function calls are completed and, thus, my this.result object is completed.

However, I don’t know how to manage this. I tried to work with counters, to know when the last object[i] is handled and when the last object[i][o] was handled to call goToNextPage() afterwards. But this doesn’t work properly, if the last object fails one if statement. Then goToNextPage() is called, while the async function of the second to last object is still running.

And I can’t just call goToNextPage() within

this.asyncFunction().then( data => {
    this.result.push(data)
    // call it here, if last element is reached
});

since then it would not be called if the last element doesn’t meet one of the IF statements.

I hope it’s understandable what my problem is and that there is a solution for this…

Thank you for your time!


Wait for navParams.get to finish before loading page
#2

You search() shoud return Promise that shoud be resolved when necessary.

search().then(doSomething);

#3

Nice idea. However, doesn’t that lead to the same problem? Where do I say resolve(...) then within the above structure of search()?


#4

This forum about Ionic1,2 not JS/TS itself, for future such thing is better to ask on Stackoverflow, but here is solution if understand you right:

search(){
   let promises = [];
    // for loop 1
    for (let i in object){
        // IF statement 1
        if (condition1){
            // for loop 2
            for (let o in object[i]){
                // IF statement 2
                if (condition2){
                    let promise = new Promise((resolve, reject) => { 
                        this.asyncFunction().then( data => resolve(data)); 
                    });
                    promises.push(promise); 
                }
            }
        }
    }

    return new Promise((resolve, reject) => {
        if(!promises.length){
            reject('Something went worng!');
        }else{
            Promise.all(promises).then((results) => {
                this.result = this.result.concat(results);
                resolve('We done it!');
            });
        }        
    });         
  }

  search().then(
    successText => {
        console.log(statusText);
        console.log(this.result);
    },
    failText => {
        console.log(failText);
    }
  );

#5

Thank you, @Koleman. I highly appreciate that.
The Promise.all(promises).then(...) made it! Thank you so much!