Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

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

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;
  }

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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();

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

So first,

  • 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
}

Then, call as follows :

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

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...