Mands
Mands

Reputation: 221

How to hold a http request without failing until response coming in Angular

This is my http API request this.dashboardServiceHandler.getTxnInfo([], params). API returns a response like after 2 min.In here i am trying to hold my request until reponse coming.But in network tab it shows pending for a long time and it fails. How could i hold my request until response comes.

delayForFiveSeconds = () => timer(135000);

getBookingInfo(dateType: string) {
  const delayForFiveSeconds = () => timer(135000);
  const params = [];

  params.push({code: 'dateType', name: dateType});
  params.push({code: 'from', name: '2019-01-01'});
  params.push({code: 'to', name: '2019-01-31'});

  // this.timeout(4000);
  return  this.ServiceHandler.getTxnInfo([], params);
}

In this class i am calling the backend API.

export class ServiceHandler {
    getTxnInfo(headers: any[], params: any[]) {
    return this.apiService.get(environment.rm_url + 'rm-analytics-api/dashboard/txn-info', headers, params);
  }
}

getBookingDetails() {
  this.delayForFiveSeconds().subscribe(() => {this.getBookingInfo('BOOKING').subscribe(
    bookings => {
      console.log(bookings);
    });
  });
}

Upvotes: 0

Views: 711

Answers (2)

Sandman
Sandman

Reputation: 1569

You can use the timeout operator of rxjs, including it in a pipe with timeout:

import { timeout, catchError } from 'rxjs/operators';
import { of } from 'rxjs/observable/of';

...
getTxnInfo(headers: any[], params: any[]) {
  this.apiService.get(environment.rm_url + 'rm-analytics-api/dashboard/txn-info', headers, params)
   .pipe(
      timeout(20000),
      catchError(e => {
        return of(null);
      })
    );
}

Using it:

  this.ServiceHandler.getTxnInfo([], params).subscribe(
    txnInfos => {
      console.log(txnInfos );
  });

Upvotes: 1

Nayak
Nayak

Reputation: 772

RxJS has a timeout operator. Probably you can use that to increase the timeout

getBookingInfo(dateType: string) {
  ...
  return  this.ServiceHandler.getTxnInfo([], params).pipe(
    timeout(10*60*1000) // 10 minutes
  );
}

And then you can update the calling function to

getBookingDetails() {
  this.getBookingInfo('BOOKING').subscribe(
    bookings => {
      console.log(bookings);
  });
}

Upvotes: 1

Related Questions