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

How to chain .then functions and callback success function in Angular.js

我正在尝试链接嵌套的 .then 函数并调用成功函数,但回调是在开始时调用。

//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'正在调用第一个 .then 函数本身。我想将此服务调用称为 que

如有任何帮助,我们将不胜感激。

您需要 return 您创建的承诺才能正确链接它们。请记住,当您使用 .then() 时,您并不是在修改承诺,而是在链中构建一个新承诺。

你的承诺代码 returned(格式化我的):

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 }对象只是为了流量控制而存在的,成功路径错误路径[=]自然可以提供48=] 的承诺 returned 由 fn();

此外,有了 promises,就没有必要将回调传递给 fn() - 事实上,这样做被认为是不好的做法。

首先,

  • 彻底清除 callback
  • return 每个低级函数的承诺
  • 简化成功链接
  • 清除不必要的错误处理程序
function fn() {
    return fn1().then(call1);
}
function call1() {
    return fn2().then(call2);
}
function call2() {
    return fn3().then(lastfunction);
}
function fn1() {
    //some code that returns a promise
}
function fn2() {
    //some code that returns a promise
}
function fn3() {
    //some code that returns a promise
}

然后调用如下:

fn().then(function(response) {
    // success callback (your "200" condition)
    // show output;
}).catch(function(error) {
    // error callback (your "500" condition)
    // show error;
});

response var 将是 lastfunction() 交付的任何内容。如果您希望 responsefn1()fn2()fn3() 所交付但 lastfunction() 尚未交付的内容的某种聚合,则会出现问题。该问题已得到全面解决

error var 将是执行fn() 过程中出现的第一个Error,不会丢失任何信息; error.messageerror.status(如果存在)可以是 read/displayed.