2016-06-02 4 views
0

Ich habe 3 Web-Anfragen, die über $ http tun, diese Petition sind in Funktionen (function1(), function2(), function3()). Ich möchte die Reihenfolge anpassen, in der all diese Anfragen ausgeführt werden.

object.function1().then(function() { 
//result of petition $http of function1(); 
}); 

object.function2().then(function() { 
//result of petition $http of function1(); 
}); 

object.function3().then(function() { 
//result of petition $http of function2(); 
}); 

sie alle versuchen, zur gleichen Zeit zu laufen. Manche Anfragen dauern länger als andere, weil sie mehr JSON-Objekte erhalten. Ich will laufen, um zu starten:

function1(); //first 
function2(); //second 
function3(); //three 
+1

sie in '.then' Handler aufrufen .. – Rayon

Antwort

-1

Rufen Sie die Funktionen in der anderen Funktion Callback wie folgt aus:

object.function1().then(function() { 
    //result of petition $http of function1(); 

    object.function2().then(function() { 
     //result of petition $http of function1(); 

     object.function3().then(function() { 
      //result of petition $http of function2(); 
     }); 
    }); 

}); 
+2

Sie sollten den "Turm des Schicksals"vermeiden: https://gist.github.com/sumtraveller/2a42b930dba2375c331e – JLRishe

5

Sie benötigen eine ordnungsgemäße Verwendung der .then() Methode zu machen:

object.function1().then(function(result) { 
    //result of petition $http of function1(); 

    return object.function2() 
}).then(function (result) { 
    //result of petition $http of function2(); 

    return object.function3(); 
}).then(function (result) { 
    //result of petition $http of function3(); 
}); 
0

Sie können dies versuchen.

$http.get('URL1').success(function(data){ 
    $http.get('URL2').success(function(data){ 
     $http.get('URL3').success(function(data){ 
      console.log('Done'); 
     } 
    } 
});