【发布时间】: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