So verketten Sie .then-Funktionen und Callback-Erfolgsfunktion in Angular.js

Ich versuche, verschachtelte .then-Funktionen zu verketten und die Erfolgsfunktionen aufzurufen, aber ein Rückruf ruft beim Start selbst auf.

//public method fn
function fn(callback) {
//calling the 1st API request
fn1()
  .then(function(response) {
    //2nd API request function
    call1(response);
  }, function(error) {
    return $q.reject({
    responseStatus: error.status
  });

  })
  // Returning response
  .then(function(response) {
    callback({
    responseStatus: 200
    });
  }, function(error) {
    callback({
      responseStatus: 500
    });
  });
}

function call1(response) {
  //2nd API
  fn2()
    .then(function(response) {
     //3rd API request function
        call2(response);
      }, function(error) {
        return $q.reject({
        responseStatus: error.status
      });
    });
}


function call2(response) {
  //3rd API request 
  fn3()
    .then(function(response) {
        return lastfunction();
      //here i need to callback the success  response status
      }, function(error) {
        return $q.reject({
        responseStatus: error.status
      });
    });
}


function fn1(){
 //some code 
 }
function fn2(){
//some code 
}
function fn3(){
//some code 
}

//Controller

//i will show response status callback here

if(response.status ==200){
  show output;
 }
 else{
  //response 500
  show errors;
  }

rundsätzlich muss ich "200" Antwortstatus an andere Controller zurückrufen, wenn alle Serviceaufrufe erfolgreich sind, und selbst wenn eine Anforderung fehlgeschlagen ist, muss ich "500" senden. mit meinem Code ruft 'Antwortstatus' 200 'mit der ersten .then-Funktion selbst auf. Ich möchte diesen Service als que @ anruf

Jede Hilfe wäre dankbar.

Antworten auf die Frage(4)

Ihre Antwort auf die Frage