Search code examples
javascriptasync-awaitangular7

wait/ stop in code execution until data getting from server


Component-1 has method-A when it get call in subscription, I need array list to check some conditions which is only get from method-Z in component-2. The data will get from DB so i need to wait Method-A condition call untill the arraylist will get.

problem explain in flow chart


Solution

  • Here, checkArrayList() is method which check weather the arraylist will gets data. this.shareDataService.isLoading flag is true/false in method-z calls. when data load from db this.shareDataService.arrayList will get updaded.

    let checkArrayList = async isLoad => {
                do { 
                    isLoad = this.shareDataService.isLoading;// true/false in method-z  
                    if (isLoad) {
                        await new Promise(r => setTimeout(r, 500))          
                    } else {  
                        console.log(this.shareDataService.arrayList); //  data will get here
                    }
                }
                while (isLoad) //when isload false while loop will break.
            }
    

    checkArrayList(this.shareDataService.isLoading); // calling function in method-A