ネストされた.then関数を連鎖させて成功関数を呼び出そうとしていますが、コールバックは開始自体を呼び出しています。Angular.jsの関数とコールバック成功関数をチェーンする方法
//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」応答ステータスをコールバックする必要が成功していると、1つの要求が失敗した場合でも、私は「500」を送っする必要があります。私のコードの応答ステータス '200'は最初の.then関数自体を呼び出しています。このサービスコールをqueとして呼びたい
何か助けていただければ幸いです。
しかし、私の応答がcall2関数で失敗した場合、この方法ではエラーコールバックが正しく呼び出されません。 – rUI7999
低レベル関数からの約束を返すことによって、エラー状態は自然に上向きに伝播します。したがって、 'call2()'やその他の場所でエラーが発生した場合は、エラーハンドラに入ります。 –
これは魅力的な作品でした! – rUI7999