Javascript 如何在Angular.js中链接.then函数和回调成功函数

Javascript 如何在Angular.js中链接.then函数和回调成功函数,javascript,angularjs,promise,chaining,Javascript,Angularjs,Promise,Chaining,我正在尝试链接嵌套的。然后调用函数并调用成功函数,但是回调调用的是起始函数本身 //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({

我正在尝试链接嵌套的。然后调用函数并调用成功函数,但是回调调用的是起始函数本身

//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;
  }
基本上,我需要回调200响应状态的其他控制器对所有服务调用都是成功的,即使一个请求是失败的,我需要发送500。在我的代码中,“响应状态”200正在调用第一个。然后是函数本身。我想把这项服务称为que


任何帮助都将不胜感激。

您需要返回您创建的承诺,以便正确链接它们。使用时请记住,这样你就不是在修改承诺,而是在链中构建一个新的承诺

您的带有承诺的代码返回了我的:

function fn(callback) {
  return fn1()
    .then(function(response) {
      return call1(response);
    }, function(error) {
      return $q.reject({
      responseStatus: error.status
    });

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

function call1(response) {
  return fn2()
    .then(function(response) {
        return call2(response);
      }, function(error) {
        return $q.reject({
        responseStatus: error.status
      });
    });
}


function call2(response) {
  return 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 
}
您的{responseStatus:x}对象的存在仅仅是为了流控制的目的,这可以通过fn返回的承诺的成功路径和错误路径自然提供

此外,有了承诺,就没有必要向fn传递回调——事实上,这样做被认为是不好的做法

所以首先,

彻底清除回调 从每个低级函数返回承诺 简化成功链接 清除不必要的错误处理程序 然后,按如下方式调用:

fn().then(function(response) {
    // success callback (your "200" condition)
    // show output;
}).catch(function(error) {
    // error callback (your "500" condition)
    // show error;
});
响应变量将是最后交付的函数。如果您希望响应是由fn1、fn2、fn3交付的、而lastfunction尚未交付的内容的聚合,则会遇到一个问题。这一问题已得到全面解决


错误var将是执行fn过程中发生的第一个错误,不会丢失信息;可以读取/显示error.message和error.status(如果存在)。

感谢您的回复!是的,我正在按顺序正确地链接它们,但是我需要显示成功和失败时回调的响应状态errors@rip1699你是说这个代码不能解决这个问题吗?当您使用它时,您看到了什么行为?同样的行为,我只是忘记在代码中提到return语句。基本上,响应在第一个函数之后调用,但如果我的响应在call2函数中失败,那么错误回调就不能用这种方法正确调用。通过从低级函数返回承诺,错误状态自然向上传播。因此,无论在哪里发生错误,无论是call2还是其他地方,它都会在错误处理程序中结束。@rUI7999如果call2发生故障,您到底不喜欢什么?@Roamer-1888是否可以压缩此模式,以便只需要N个函数,而不需要N对函数?例如,我们真的需要fn2和call2吗?
fn().then(function(response) {
    // success callback (your "200" condition)
    // show output;
}).catch(function(error) {
    // error callback (your "500" condition)
    // show error;
});