timeout for $ http request (fetch for 5 seconds, if not, return 'error')

0

I'm setting a $ http request for zip code, so sometimes it takes too long for the API to return a response, and the user is stuck with continuing to checkout.

The function worked, but when I tried to add the timeout it stopped.

FUNCTION:

  if(this.cep && this.cep.length > 7) {
    // Conneting API to get CEP Values
    $http.get('${API_URL}/carriers/correios/get-cep/${this.cep}')
    .timeout(500, () => {
      .success((address) => {
        this.CEPloading = false;
        this.CEPerror = false;
        this.validCEP = true;

        updateShippingPrice(address.uf);
        updatePrice();

        this.bairro = address.bairro;
        this.cidade = address.cidade;
        this.endereco = address.end;
        this.uf = address.uf;

        updatePrice();
      })
      .error(() => {
        this.CEPloading = false;
        this.CEPerror = true;
      });
    })

  }
    
asked by anonymous 13.08.2015 / 16:27

1 answer

1

According to the angularJS website.

Use a configurator for the http.

example below.

$scope.method = 'GET';
$scope.url = ${API_URL}/carriers/correios/get-cep/${this.cep};
$scope.timeout = 10000; (miliseconds);

$http({method: $scope.method, url: $scope.url, cache:false, timeout = $scope.timeout}).then(function(response) {
          $scope.status = response.status;
          $scope.data = response.data;
        }, function(response) {
          $scope.data = response.data || "Request failed";
          $scope.status = response.status;
      });

reference: link

    
17.08.2015 / 21:30