6
jQuery 1.9.1 약속에 문제가 있습니다. 잠재적으로 또 다른 지연을 반환하는 조건부 논리가 필요할 수 있으며 처리 방법이 확실하지 않습니다. 이것은 최선의 시도 였지만, 아래에 주석이 표시되어있을 때 else 브랜치를 눌렀을 때 나는 여전히 두 번째 .then() 함수를 사용하여 사용자에게 돌아갈 수 있기를 바라고 있습니다. 이러한 시나리오를 처리하는 방법에 대한 패턴은 무엇입니까?약속을 사용하여 분기 처리
storage.provision(c)
.then(function(rc){
if(rc === 0){
storage.write(c);
}else{
return options.onSuccess(rc); //how i got back to the users callbacks/promise, but this
//takes me to the .then below
}
})
//storage.write returns a promise as well, do I do another .then
// like this?
.then(function(rc){
//I was hoping this would catch, the storage.write() case, and it does, but it also catches
//the retun options.onSuccess(rc) in the else case.
options.onSuccess(rc);
})
.fail(function(e){
//handle error using .reject()
});
이 게시물은 도움이 될 수 있습니다 : http://stackoverflow.com/questions/12149993/attempting-to-break-jquery-promise-chain-with-then-fail-and-reject – mattytommo
[This post] (http : //net.tutsplus.com/tutorials/javascript-ajax/wrangle-async-tasks-with-jquery-promises/) 약속 내용을 더 잘 이해하는 데 도움이됩니다. 일반적으로 조건문을 사용하려면 약속을 변수로 저장하고 조건 내에 '성공'또는 '실패'를 실행해야합니다. 그러면 다른 방법으로 수행하는 것처럼 보입니다. – acconrad