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

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);
    }
  );
1 Like