Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
323 views
in Technique[技术] by (71.8m points)

angular how to increase the waiting time for an answer of API rest Observable RxJS

I have application en Back Java Spring Boot and in front Angular.

In front i am using a REST api to delete a file on my server but I have a 404 error even though the service has been consume and finished correctly. I pence that the problem comes from the waiting time of my response. I have a proxy that I do not find great.

So my question is there any way to increase the wait time of answer Angular Rxjs Observable?

// services
deleteFileByName(fileName: String){
    let url = this.host + '/files/' + fileName;
    console.log(url);
    console.log(this.http.delete(url, {headers: this.headers}));
/* Observable
return this.http.delete(url, {headers: this.headers}); */
    return this.http.delete(url, {headers: this.headers}).toPromise();
  }


// component
      /*Delete */
      /* Observable
      public onDeleteFile(filename) {
        let c = confirm('Are you want permanently delete ' + filename + ' ?');
        if (!c) {
          return;
        }
        this.uploadService.deleteFileByName(filename).pipe(timeout(90000)).subscribe(data => {
          console.log('Delete done');
        }, error => {
          alert(error);
        });

      }*/
    onDeleteFile(filename) {
      this.uploadService.deleteFileByName(filename).then(() => {
            alert("ok");
        }).catch((error) => {
            console.log(error);
        })
    }

// security
http.authorizeRequests().antMatchers("/appUsers/**","/appRole/**", "/allUser/**", "/appParam/**","/files/**","/upload/**").hasAuthority("ADMIN");

// Controller
@DeleteMapping(value="/files/{filename}")
public void delete(@PathVariable(name = "filename")String filename){
    logger.info("Delete file Start " + filename);
    storageService.deleteByFilename(filename);
    logger.info("Delete file " + filename + " over ");      
}

//console Java

2021-01-25 10:51:22.682 INFO 19640 --- [io-8082-exec-10] c.a.toDoList.controller.FilesController : Delete file Start image (3).png 2021-01-25 10:51:22.686 INFO 19640 --- [io-8082-exec-10] c.a.toDoList.controller.FilesController : Delete file image (3).png over

// Console Brwoser enter image description here

question from:https://stackoverflow.com/questions/65830171/angular-how-to-increase-the-waiting-time-for-an-answer-of-api-rest-observable-rx

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I have faced similar kind of issue where API took time to respond or send data, then I used Promise.

promiseFuntion(): Promise<void> {
    return new Promise((resolve: Function, reject: Function) => {
        this.apiService.userApi().subscribe(
            (res: any) => {
                // do your code
                resolve();
            },
            (error: Error) => {
                // do your code
                reject();
            }
        )
    })
}

yourFunction() {
    this.promiseFuntion().then(() => {
        // do your code and this code will execute when you API is ready
    }).catch((error) => {
        console.log(error);
    })
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...