Typescript - 在执行函数之前等待返回

问题描述 投票:0回答:1

我试图在响应完成后执行一个函数。如果没有 setTimeout() 我该如何做到这一点? 我正在上传大文件,如果文件很小,则工作正常,但如果文件很大,则等待时间不够。

 this.orderTestService.orderObservable$

.pipe(untilDestroyed(this))

.subscribe((response) => {    

  if(response){

    setTimeout( () => {

      this.service.getDocuments(); }, 1000 );

  }

 }

});
angular typescript
1个回答
0
投票

您可以使用 finalize 运算符,该运算符在可观察对象完成时执行您的函数。

this.orderTestService.orderObservable$
  .pipe(
    untilDestroyed(this),
    finalize(() => {
      this.service.getDocuments();
    })
  )
  .subscribe((response) => {
    if (response) {
      // Handle successful response 
    }
  });
© www.soinside.com 2019 - 2024. All rights reserved.