How to make a request $ http.get be completed before another in angularjs

1

I have a problem with the services in angular and it is that I am calling a function with request $ http.get inside a service that also makes request $ http.get.

This is my code when I check, if you see inside it there is a function called searchDeuda () which is what I want to finish first. How do you do that in Angular?

    function consultar(cuenta) {
               //Funcion que tambien hace una peticion $http 
                    searchDeuda();

                var config = {
                    params: {
                        cuentaNro: $scope.cuenta,
                        username: $scope.username
                    }
                };

                apiService.get('api/cuentasAsociadas/consultar/', config,
                loadCompleted,
                loadFailed);
            }

function loadCompleted(result) {

        if ($scope.conDeuda == true) {
            $location.path('/arregloPago').replace();
        } else {
            notificationService.displayDialog("Aviso", 'Cuenta no posee Deudas', "OK");
        }
    }
}



    function loadFailed(response) {
        //notificationService.showConfirm('Aviso!!!', response.data, 'OK');
        notificationService.showConfirm(this, 'Aviso!!!', response.data, 'AGREGAR CUENTA', 'OK')
                .then(function () {
                    $location.path('/cuentasAsociadas/agregarCuenta').replace();
                }, function () {

                });
    }

// Function that is within the request consult

function searchDeuda() {
        var config = {
            params: {
                CuentaNro: $scope.cuenta
            }
        };            

        apiService.get('api/ArregloPago/Deuda/', config,
        deudasLoadCompleted,
        deudasLoadFailed);
    } 



function deudasLoadCompleted(result) {
        if (result.data == 'Cuenta no posee Deudas') {
            $scope.conDeuda = false;
        } else {
            $scope.conDeuda = true;
        }
    }



  function deudasLoadFailed(response) {
        notificationService.displayError('Error al buscar deuda');
    }
    
asked by Assiel Nahum Reyes Umaña 05.09.2018 в 17:15
source

1 answer

1

You can achieve this by using a callback in searchDedua .

Edit searchDeuda to accept a function that will be executed when you have obtained the result:

function searchDeuda(callback) {
        var config = {
            params: {
                CuentaNro: $scope.cuenta
            }
        };            

        apiService.get('api/ArregloPago/Deuda/', config,function(response){
            deudasLoadCompleted(response);
            callback(response);
        },deudasLoadFailed);
 } 

Then when you go to consult, you send the callback to searchDeuda, in this case from consulting:

function consultar(cuenta) {

   // definimos el metodo
    searchDeuda(function(response){

        // cuando recibas la respuesta de searchDeuda,
        // se ejecutara esta funcion

        var config = {
            params: {
                cuentaNro: $scope.cuenta,
                username: $scope.username
            }
        };

        apiService.get('api/cuentasAsociadas/consultar/', config,
        loadCompleted,
        loadFailed);

    });
}

So then when searchDeuda gets the result, it is when apiService.get('api/cuentasAsociadas/consultar/') will be executed.

    
answered by 05.09.2018 / 17:25
source