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

**URL:** <https://forum.ionicframework.com/t/identify-when-nested-for-loops-and-if-statement-with-one-asynchronous-function-call-are-done/89974>\
**Category:** ionic-v3\
**Created:** [May 12, 2017, 7:54am UTC](https://forum.ionicframework.com/t/identify-when-nested-for-loops-and-if-statement-with-one-asynchronous-function-call-are-done/89974 "2017-05-12T07:54:56Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![Koleman](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/koleman/32/91048_2.png) [@Koleman](https://forum.ionicframework.com/u/Koleman)\
**Post date:** [May 12, 2017, 10:34am UTC](https://forum.ionicframework.com/t/identify-when-nested-for-loops-and-if-statement-with-one-asynchronous-function-call-are-done/89974/4 "2017-05-12T10:34:02Z")

</div>

This forum about Ionic1,2 not JS/TS itself, for future such thing is better to ask on [Stackoverflow](http://stackoverflow.com/questions/tagged/javascript), but here is solution if understand you right:

```auto
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);
    }
  );

```

---

_[View the full topic](https://forum.ionicframework.com/t/identify-when-nested-for-loops-and-if-statement-with-one-asynchronous-function-call-are-done/89974)._
