Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

152
Views
for loop gets completed before Observable in Angular

I am calling a function onRoute() which returns Promise. function will sync my data to API multiple times according to the length of data and returns back with a string message every time. I wanna store it but the problem is for loop gets completed before getting message from API server.

please help me out.

 syncOnRoute() {
    return new Promise((resolve, reject) => {
      this.auth.database.executeSql('select * from onroute where HaveData=?', 
        ['yes']).then((res) => { //will get 3 data from Database
       
        for (let i = 0; i < res.rows.length; i++) { // will execute 3 times
                        
                let params = new HttpParams()
                  .set('CLAIMGUID', res.rows.item(i)['CLAIMGUID'])
                  .set('JOBGUID', res.rows.item(i)['JOBGUID'])
                  .set('Latitude', res.rows.item(i)['Latitude'])
                  .set('Longitude', res.rows.item(i)['Longitude'])
                  .set('OnRouteDT', res.rows.item(i)['OnRouteDT'])
                  .set('Desc', this.auth.getdateformatV2())
                  .set('title', 'onroute');
                  this.auth.httpSend('Service_SaveOnRoute', params, 'post').subscribe((msg) => {
                    console.log('##SUCCESS ONROUTE SYNC' + JSON.stringify(msg)); // getting return msg from API

                  }, 
                  (err) => {
                    console.log('##SUCCESS ONROUTE SYNC' + JSON.stringify(err));
                     
                  }
                );
} // for loop gets completed before geting return msg from API
                
                   
x

Here is my httpSent() which is observable

public httpSend(endPoint:string, params:HttpParams , method: string){
    
    switch(method){

      case 'post':{
        return  this.httpClient.post<any>("https://ws-v3test.ventureprise.cloud/Service1.asmx/"+endPoint,params);

      }
      break;
      case 'get':{
        return  this.httpClient.get<any>("https://ws-v3test.ventureprise.cloud/Service1.asmx/"+endPoint);

      }
    }
  }

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I guess you can change your subscribe to toPromise and use for...of with await

Alternative you can put all this.auth.httpSend('Service_SaveOnRoute', params, 'post') to an array and after loop is end you can wait for all subscribe

I hope it helps you! Enjoy programming!

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for-await...of

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!