【问题标题】:How to chain .then functions and callback success function in Angular.js如何在 Angular.js 中链接 .then 函数和回调成功函数
【发布时间】:2016-09-07 17:53:25
【问题描述】:

我正在尝试链接嵌套的 .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

任何帮助将不胜感激。

【问题讨论】:

    标签: javascript angularjs promise chaining


    【解决方案1】:

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

    您返回的带有承诺的代码(格式化我的):

    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 
    }
    

    【讨论】:

    • 感谢您的回复!是的,我按顺序正确链接它们,但我需要在成功和错误时显示回调的响应状态
    • @rip1699 你是说这段代码不能解决问题吗?您在使用时看到的行为是什么?
    • 同样的行为,我只是忘了在代码中提到 return 语句。基本上响应是在第一个函数本身之后调用
    【解决方案2】:

    您的{ responseStatus: x } 对象仅用于流控制,可以由@987654323 返回的promise 的成功路径错误路径 自然提供@;

    此外,有了 Promise,就无需将回调传递给 fn() - 实际上这样做被认为是不好的做法。

    首先,

    • 彻底清除callback
    • 从每个低级函数返回一个承诺
    • 简化成功链接
    • 清除不必要的错误处理程序
    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() 交付的内容的某种聚合,则会出现问题。该问题已得到全面解决here

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

    【讨论】:

    • 但是,如果我的响应在 call2 函数中完全失败,则错误回调不会用这种方法正确调用。
    • 通过从低级函数返回 Promise,错误状态自然会向上传播。因此,无论在哪里发生错误,call2() 或其他地方,它都会在错误处理程序中结束。
    • @rUI7999 如果 call2() 发生故障,你到底不喜欢什么?
    • @Roamer-1888 这种模式可以压缩成只需要 N 个函数而不是 N 个 pairs 函数吗?例如,我们真的需要both fn2() 和 call2() 吗?
    猜你喜欢
    • 2018-03-13
    • 2020-03-15
    • 1970-01-01
    • 1970-01-01
    • 2020-07-31
    • 1970-01-01
    • 2020-10-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多