Jquery 如何在chained.then()函数中更改承诺解析结果

Jquery 如何在chained.then()函数中更改承诺解析结果,jquery,promise,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);

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

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)
我想知道我如何解决它(转向成功处理程序)沿。然后链


通过在任何需要的时间点返回已解决的承诺,表示感谢:

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
    });
is的相关部分(重点矿山)

这些过滤器函数可以返回要传递给的新值 承诺的
.done()
.fail()
回调,或者它们可以返回 另一个可以观察到的物体(延迟、承诺等)将通过 其已解决/拒绝的状态和值与承诺的回调有关


可能值得注意的是,可以使用
$.when()
$.when(value)
生成解析的jQuery承诺,其中任何一个都可以从错误处理程序返回,以沿成功路径发送承诺链。请注意,从下一个版本的jQuery开始执行
return x+1
也可以。@BenjaminGruenbaum:我不知道这一点。有什么地方我可以读到更多关于它的信息吗?@Jon我最近写的关于它的参考文献是,但是jQuery问题跟踪器也是一个阅读它的好地方。