How to expect an HTTP request to end at Angular 5

Asked

Viewed 86 times

-1

I am creating a method that makes an HTTPSERVICE request in my API but I need to finish this request before continuing the rest of my Method, as I could force Angular to wait for the end of my request

Here is the requisition

this.httpService.wait();
this.httpService
  .post('/custom/pedido/gerar-financeiro-pedido', listasEnviar)
  .subscribe(result => {
    if (result.error || result.nfeIds.length == 0) {
      this.exibirMensagem(result);
      this.httpService.done();
      return;
    }

    const listasEnviar = {
      pedidos: result.nfeIds,
      empresa_id: this.dblinkedSessionService.empresa.id,
      usuario: this.sessionService.loggedUser.id
    };

    this.httpService.wait();
    this.httpService
      .post(`${this.configService.flaskFinUrl}/flaskfin/boleto/post-boleto`, listasEnviar)
      .subscribe(result => {
        if (result.error) {
          this.exibirMensagem(result);
          this.httpService.done();
          return;
        }

        const httpOptions2 = {
          headers: new HttpHeaders({
            'Authorization': this.configService.bearerApi,
            'Cache-Control': 'no-store, max-age=0'
          }),

          responseType: 'blob' as 'json'
        };
        this.httpService.wait();
        this.httpClient
          .get(`${this.configService.flaskFinUrl}/flaskfin/boleto/get-boleto-zip(${result.id})`, httpOptions2)
          .subscribe(res => {                
            let link = document.createElement('a');
            let url = window.URL.createObjectURL(res);
            link.href = url;

            let fileName = result.id
            link.download = fileName;
            link.click();

            window.URL.revokeObjectURL(url);
            this.httpService.done();
          },
            error => this.httpService.handleError(error, () => this.inicializar()),
            () => this.httpService.done()
          );
      },
        error => this.httpService.handleError(error, () => this.inicializar()),
        () => this.httpService.done()
      );
  },
    error => this.httpService.handleError(error, () => this.inicializar()),
    () => this.httpService.done()
  );
});

1 answer

0

Voce can make an asynchronous function that returns a promisse

private async ngOnInit(){ await this.getPermission(); }

private getPermission(){ Return new Promise(async (resolve, Reject) => { // LOGICA resolve(true); // WHEN IT FALLS HERE RETURNS }) }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.