如何在链接.then()函数中更改promise解析结果

时间:2015-01-24 01:19:19

标签: jquery promise

现在,我有一个被拒绝的承诺链:

dfd = $.Deferred();

dfd
    .then(function(){}, function(x) {
        return x + 1; // 2
    })
    .then(function(){}, function(x) {
        return x + 1; // 3
    })
    .then(function(){}, function(x) {
        log('reject ' + x); // 3
        return x + 1; // 4
    });

dfd.reject(1)

我想知道如何沿着.then链解决它(引导成功处理程序)?

由于

1 个答案:

答案 0 :(得分:3)

通过在任何所需的点返回已解决的承诺:

dfd
    .then(function(){}, function(x) {
        return x + 1; // 2
    })
    .then(function(){}, function(x) {
        return $.Deferred().resolve(x + 1); // switch to success
    })
    .then(function(){}, function(x) {
        log('reject ' + x); // this will never happen now
    });

docs的相关部分是(强调我的)

  

这些过滤器函数可以返回要传递的新值   承诺的.done().fail()回调,或者他们可以返回   另一个可观察的对象(Deferred,Promise等)将通过   它的解析/拒绝状态和值到promise的回调。

相关问题