promiseの失敗をキャプチャし、それを成功に変換する場合は、 thenのfailFilterを使用して、次のように解決されたpromiseを返すことができます。
deferredCall.then(function(answer) {
// this is success. you might transform the answer here.
return transformed;
}, function() {
// this is a fail. you might resolve the fail with an empty object.
return $.Deferred().resolve({}).promise();
});
これを行うことで、チェーンが中断することなく障害を超えて継続できるようになります。
したがって、あなたの例では、これを行うことができます:
$.when([
a.ajax(),
b.ajax().then(function(answer) {
return answer;
}, function() {
return $.Deferred().resolve({}).promise();
}),
c.ajax()
]).then(function(results) {
// etc.
});
例2:私のアプリケーションでは、特定のエンティティのリレーショナルデータを取得し、404がそのような関係が存在しないことを示す可能性を考慮して使用することがあります。
getEntity(id).then(function(entity) {
return getAssociation(id).then(function(association) {
entity.association = association;
return entity;
}, function() {
entity.association = null;
return $.Deferred().resolve(entity).promise();
});
}).done(function(entity) {
// etc.
});
古い回答では、パイプ方式を使用することを提案していることに注意してください。このメソッドは、jQuery1.8で非推奨になりました。