【发布时间】:2013-03-10 05:02:45
【问题描述】:
我对 jQuery 1.9.1 Promise 有疑问,我可能需要返回另一个 deferred 的条件逻辑,但我不知道如何处理它。这是我最好的尝试,但正如下面的 cmets 所示,当我点击 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()
});
【问题讨论】:
-
这篇文章可能对你有所帮助:stackoverflow.com/questions/12149993/…
-
This post 将帮助您更好地理解承诺。一般来说,如果你想使用条件,你应该将承诺保存为一个变量并在你的条件中执行你的
thensuccess或fail,你似乎在做相反的事情。
标签: javascript jquery asynchronous promise