Sunday 4 October 2020

Unable to set interval up to max 2 min or until condition met using angular

here what my scenario is i have 2 api's apiOne and apiTwo and when ever i call the apiOne is should give response and if the response is success then i have to send this repsonse to apiTwo as param then apiTwo will give another response in that i may get like "created" ,"in_progress" . here the issue is

  1. How can i call the apitwo using interval for every 3 seconds until i get the response as "in_progress" and if i didnt get the response as like above then i need to poll the apiTwo till max 2 min and cancel the call. if i get the response as in_progress then i need to stop the interval or max 2 min cancel the interval or subcription.

  2. I already wrote the code in nested way but it is not efficient .

below is my code

 initiate() {
    this.showProgress = true;
    
    const data = {
      id: this.id,
      info: this.Values.info,
    };

    // First Api call
    
    this.userServ.start(data).subscribe(res => {
     
      this.Ids = res['Id'];
      if (this.Ids) {
      
      
      // Second Api call
      
        this.Service.getStatus(this.Ids).subscribe(resp => {

          if (resp) {
            
            this.Status = res['Status'];
            
            // if resp is In_Progress
            
            if (this.Status === 'In_Progress') {
              this.Start();
            } else {

            // if resp is not In_Progress then i get the response i am calling the api
            
              this.intervalTimer = interval(3000).subscribe(x => {

                this.Service.Status(this.Ids).subscribe(ress => {
                 
                  this.Status = ress['Status'];
                  if (this.Status === 'In_Progress') {
                    this.delayStart();
                    this.intervalTimer.unsubscribe();
                  }
                });
              });

            }
          }

        }, err => {
          console.log(err);
        });
      }

    }, error => {
      console.log(error);
    });

  }


from Unable to set interval up to max 2 min or until condition met using angular

No comments:

Post a Comment