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

后端 未结 2 1980
一个人的身影
一个人的身影 2020-12-20 09:41

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(c         


        
2条回答
  •  悲&欢浪女
    2020-12-20 10:05

    You need to return the promises you create in order to chain them properly. Keep in mind when you use .then() you aren't modifying a promise, you're constructing a new one in the chain.

    Your code with promises returned (formatting mine):

    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 
    }
    

提交回复
热议问题