如何在Angular.js中链接.then函数和回调成功函数 [英] How to chain .then functions and callback success function in Angular.js

查看:132
本文介绍了如何在Angular.js中链接.then函数和回调成功函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to chain nested .then functions and call the success functions, but call back is calling in the starting itself.

//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

Basically i need to callback "200" response status to other controller on all service calls are successful and even if one request is failed i need to sent "500". with my code 'response status '200' is calling with the first .then function itself. I want to call this service calls as que

任何帮助都将不胜感激。

Any help would be appreciated.

推荐答案

您的 {responseStatus:x} 对象仅用于流量控制,可以通过成功路径自然提供 fn()返回的承诺的错误路径 ;

Your { responseStatus: x } object exists solely for the purpose of flow control, which can be provided naturally by the success path and error path of a promise returned by fn();

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

Also, with promises, there's no need to pass a callback to fn() - indeed it is considered bad practice to do so.

首先,


  • 清除回调全部通过

  • 从每个低级函数返回一个承诺

  • 简化成功链接

  • 清除不必要的错误处理程序

  • purge callback all through
  • return a promise from each low level function
  • simplify success chaining
  • purge unnecessary error handlers
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;
});

响应 var将是 lastfunction()已发送。如果您希望 response 成为 fn1(),<$ c所提供内容的汇总,则会出现问题$ c> fn2(), fn3()尚未由 lastfunction()此处

The response var will be whatever lastfunction() delivered. You have an issue if you want response to be some aggregation of what is delivered by fn1(), fn2(), fn3() that is not already delivered by lastfunction(). That issue is comprehensively addressed here.

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

The error var will be the first Error to occur in the course of executing fn(), with no loss of information; error.message and error.status (if it exists) can be read/displayed.

这篇关于如何在Angular.js中链接.then函数和回调成功函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆