use nested $ http requests angular 1

0

Hello good morning I have a project with Ionic 1 where I have to make different queries and many requests in a single click (when synchronizing) where I get a list of people first and the result I get I am consulted all your data and at the same time sent all your information

for(var i=0; i<resutl.length; i++){
    send(resutl, id_con_person);
    //aqui llamo a la funcion send que a la vez esta funcion llama otras 
      funciones 
}

How can I do it synchronously with angle 1 or what is the correct way to do this procedure? I would really appreciate it, greetings.

function send(resutl, id_con_person){
query="SELECT * FROM identification where id_con_person=?";
 $cordovaSQLite.execute(db,query,[this_idPerson]).then(

var req = {method: 'POST',url: base_url_services, 
           data:$httpParamSerializer({xxxx.stringify(xxx)}),
           headers: {'Content-Type': 'application/x-www-form-urlencoded'}
                                }

$http(req).success(function(data, status, headers, config) 
{
    if(data[0].status==="SUCCESS"){
            insertXXX(data[0].xxx)
            insertXXX(data[0].xxxx);
    }else{}

}).
error(function(data, status, headers, config) 
{
   alert("Error");
  });introducir el código aquí

}
,function(error){
  alert(JSON.stringify(error));
});

}


function insertXXX(){
query="SELECT * FROM xxxx ";
 $cordovaSQLite.execute(db,query,[]).then(

var req = {method: 'POST',url: base_url_services, 
           data:$httpParamSerializer({xxxx.stringify(xxx)}),
           headers: {'Content-Type': 'application/x-www-form-urlencoded'}
                                }

$http(req).success(function(data, status, headers, config) 
{
    if(data[0].status==="SUCCESS"){

    }else{}

}).
error(function(data, status, headers, config) 
{
   alert("Error");
  });introducir el código aquí

}
,function(error){
  alert(JSON.stringify(error));
});
}
    
asked by SRF SRF 14.07.2017 в 17:01
source

1 answer

0

Remember that service calls REST are asynchronous. But you can nest calls with a promise using then instead of success

Here is an example:

$http.get(req).then(function(response) {
     // success 
     if(response.data[0].status==="SUCCESS"){
         $http.get(req2).then(function(response) {

         });
     }else{}

}, function(response) {
    // error handler
});

You can see an explanation in English about the difference between "then" and "success" here

    
answered by 18.07.2017 в 08:43