2016-08-18 5 views
3

다음 코드를 사용하여 약속을 거부하면 첫 번째 console.log가 표시되지만 두 번째 console.log도 표시됩니다. 이것은 거절이 다음 "then()"에만 영향을주기 때문에 예상됩니다.콘솔에 오류 메시지를 보내지 않고 체인을 중지 할 수 있습니까?

질문은 체인에서 뛰어 내릴 수 있도록 루프의 "끊기"와 같은 연산자가 있습니까? 빠른 응답은 예기치 않은 reject() 또는 throw()와 함께 예가 될 수 있지만 두 방법 모두 콘솔에 오류 메시지를 보냅니다 (전체 코드 실행이 중단됩니다).

, 그것은 "깨끗하게"행해질 수 있습니까?

*, 나의 첫 번째 가정은가/오류를 거부 잡기하여이 트릭을 할 것이라고했다,하지만 경우에 당신이 then 방법에 두 번째 인수 (onRejected 기능)을 통과하면

Promise.resolve() 
.then(function(){ 
    do stuff 
    return Promise.resolve(); 
}) 
.then(function(){ 
    if (a === 'this') 
    return Promise.resolve(); 
    else 
    // is there any "break" equivalent here? 
    return Promise.reject(); 
}) 
.then(function(){ 
    console.log('seen if not rejected'); 
},function(){ 
    console.log('seen if rejected'); 
}) 
.then(function(){ 
    console.log('seen all the time'); 
}); 
+0

'ReferenceError가 : 문제가 아닌 정의되지 않은 (...)' –

+1

@CoryDanielson. 그들은 거절 된 약속을 되 돌리는 것이 왜 사슬을 끝내지 않는지 묻습니다. –

+0

@CoryDanielson 당신은'.then()'을 계속해서 호출하기 때문에 이것을 의사 코드 – Gerard

답변

1

.then(success, error) 콜백 약속을 반환합니다. 이다

:

Promise.reject() 

.then(function() { 
    // won't be called, as above promise is rejected 
}, function() { 
    // will be called, and returns a resolved promise by default; 
    // if you want the next error callback to be called, 
    // you need to return a rejected promise or to throw an error 
}) 

.then(function() { 
    // 1: called if above .then returns a resolved promise 
}, function() { 
    // 2: called if above .then returns a rejected promise or throws an error 
}) 
1

아니다 그 거부는 실제로 잡힌다. 그리고 약속을 되 돌리면서 연쇄를 계속할 수 있습니다.

Promise.reject() 
    .then(function(){ 
    console.log('resolved'); 
    }) 
    .then(function(){ 
    console.log('you cannot see me'); 
    }) 

// Uncaught (in promise) 

를 그리고이 onRejected에 의해 체포되어 있지 않은 경우 예, 당신은 예외를 catch 수 있습니다

당신이 당신의 셋째 thenonRejected 기능을 제거하는 경우 예를 들어, 콘솔의 캐치되지 않는 예외를 볼 수 있습니다 :

Promise.reject() 
    .then(function(){ 
    console.log('resolved'); 
    }) 
    .catch(function(){ 
    console.log('you can see me'); 
    }) 

// you can see me 


Promise.reject() 
    .then(function(){ 
    console.log('resolved'); 
    }, function(){ 
    console.log('rejected'); 
    }) 
    .catch(function(){ 
    console.log('you cannot see me'); 
    }) 

// rejected 

내 경험에 비추어 볼 때, 실제 비즈니스 시나리오에서는 일반적으로 관련없는 논리를 분리하려고합니다. 당신이 거절을 잡기 때문에

Promise.reject() 
    .then(function(){ 
    console.log('resolved'); 
    }, function(e){ 
    console.log('rejected'); 
    throw e 
    }) 
    .catch(function(){ 
    console.log('you can see me'); 
    }) 

// rejected 
// you can see me 
-1

그건 : 그래서 우리는 onRejected에서 거부를 잡은 것, 무언가를, 다음 (자체 또는 then 체인의 하나) catch 메소드에 예외를 throw합니다. 아래의 두 블록은 동일합니다

예 1

var stuff = function() { 

}; 
var doStuff = function() { 

}; 
var a = 'notThis'; 

Promise.resolve() 
    .then(function() { 
     doStuff(); 
     return Promise.resolve(); 
    }) 
    .then(function() { 
     if (a === 'this') 
      return Promise.resolve(); 
     else 
      return Promise.reject(); 
    }) 
    .then(function() { 
     stuff(); 
    }, function() { 
     console.log('rejected, end of chain'); 
    }) 
    .then(function() { 
     console.log('only executed if not rejected'); 
    }); 

예 2

var stuff = function() { 

}; 
var doStuff = function() { 

}; 
var a = 'notThis'; 

Promise.resolve() 
    .then(function() { 
     doStuff(); 
     return Promise.resolve(); 
    }) 
    .then(function() { 
     if (a === 'this') 
      return Promise.resolve(); 
     else 
      return Promise.reject(); 
    }) 
    .then(function() { 
     stuff(); 
    }) 
    .catch(function() { 
     console.log('rejected, end of chain'); 
    }) 
    .then(function() { 
     console.log('only executed if not rejected'); 
    }); 
+2

[아니오 , 그들은 아니에요] (http://stackoverflow.com/q/24662289/1048572). – Bergi

+0

어쩌면 내가 틀렸어. 왜 설명 할 수 있니? – Adam

+0

링크 된 답변에 설명되어 있습니다. 당신이 뭔가를 이해하지 못하면, 거기에 제발. – Bergi