Re:https://github.com/tildeio/rsvp.js
我有一个名为doSomething()的函数,它执行一段时间的操作,然后返回一个RSVP.Promise。然后,使用返回的承诺注册成功和失败回调链(请参见下面的代码)。我期望的行为是,如果承诺实现了,在承诺中注册的成功回调链将被触发,如果承诺被拒绝(失败),失败回调链将被触发。
当承诺实现时,我得到了预期的行为,但当承诺被拒绝时,我得到了不同于我预期的行为。也就是说,将成功回调链接起来,并将一个成功回调的输出传递给链中的下一个成功回调。但是,失败的回调似乎没有链接起来。它们的行为与try/catch块中的捕获几乎类似(请参阅下面的代码和输出)。
有人能解释一下这种行为吗?这真的是它应该工作的方式吗?还是这是rsvp.js处理一个被拒绝/失败的承诺的方法中的一个错误,这个承诺中有一系列的失败回调被注册了?我正在读“承诺”/“A++规范”来尝试解决这个问题,但如果有人对此了如指掌,我很想听听你的解释。提前谢谢。
小提琴手:http://jsfiddle.net/rylie/VYSj7/2/
doSomething() // returns an RSVP.Promise object
.then(
function(message) { console.log("then success 1: " + message); return "from success 1"; }, // success callback
function(message) { console.log("then failure 1: " + message); return "from failure 1"; } // failure callback
)
.then(
function(message) { console.log("then success 2: " + message); return "from success 2"; }, // success callback
function(message) { console.log("then failure 2: " + message); return "from failure 2"; } // failure callback
)
.then(
function(message) { console.log("then success 3: " + message); return "from success 3"; } // success callback
)
.then(
null,
function(message) { console.log("then failure 4: " + message); return "from failure 4"; } // failure callback
)
.then(
function(message) { console.log("then success 5: " + message); return "from success 5"; }, // success callback
function(message) { console.log("then failure 5: " + message); return "from failure 5"; } // failure callback
);**当承诺实现(成功)时,这就是我所得到和预期的产出:
then success 1: Promise fulfilled!
then success 2: from success 1
then success 3: from success 2
then success 5: from success 3 **当承诺被拒绝(失败)时,这是我得到的输出:
then failure 1: Promise rejected!
then success 2: from failure 1
then success 3: from success 2
then success 5: from success 3 **这是我所期望的(关于被拒绝或失败的承诺):
then failure 1: Promise rejected!
then failure 2: from failure 1
then failure 4: from failure 2
then failure 5: from failure 4 发布于 2013-11-01 13:44:35
您应该忘记,.then()甚至使用一个以上的参数,并使用.catch方法,这样就更有意义了。
承诺提供了一些同步代码结构的对应关系,但是当您只有一个低级别的.then()时,这一点并不是很明显。您要寻找的基本上是一系列回调/回调聚合,但这根本不是承诺的重点。
想想.catch(fn)和.then(null, fn)一样
doSomething().then(function(val) {
console.log(val);
}).catch(function(e) {
console.error(e);
});并行同步代码(假设doSomething同步返回):
try {
var val = doSomething();
console.log(val);
}
catch(e) {
console.error(e);
}多个捕获(请记住,.catch是.then(null, fn)更易读的别名
doSomething().then(function(val) {
console.log(val);
}).catch(function(e) {
return e;
}).catch(function(e){
//Will not get here ever
//because error thrown from doSomething()
//was handled in the upper catch which doesn't trow
});平行:
try {
try {
var val = doSomething();
console.log(val);
}
catch(e) {
//no need for explicit return e
}
}
catch( e ) {
//Will not get here ever
//because error thrown from doSomething()
//was handled in the upper catch which doesn't trow
}因此,现在您应该注意到,您可以通过抛出而不是返回http://jsfiddle.net/VYSj7/3/来创建预期的结果
.catch()是国际海事组织为承诺库提供的一种基本方法,也将包括在未来的内置Javascript承诺中。但是,如果没有提供这样的方法,您可以(或者应该是,不幸的是,有些实现不使用原型):
Promise.prototype.catch = function(fn) {
return this.then(null, fn);
};https://stackoverflow.com/questions/19693359
复制相似问题