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