想象一下,我有X
,Y
和Z
功能,所有返回的承诺,并准备链。我要做的是在进度完成并处理错误后通知进度。有什么更好的写作方式以及原因(后果是什么):
一种。
function my_func(index, size){
return X
.then(Y)
.then(Z)
.then(
function(data){
var dfd = new $.Deferred();
dfd.notify("progress", index / size, 'OK');
dfd.resolve(data);
return dfd.promise();
},
function(){
return handleError(arguments, size, index);
}
);
}
或者
B.
function my_func(index, size){
var dfd = new $.Deferred();
X
.then(Y)
.then(Z)
.then(
function(data){
dfd.notify("progress", index / size, 'OK');
dfd.resolve(data);
},
function(){
return handleError(arguments, size, index);
}
)
return dfd.promise();
}
另外,两者之间有什么区别?
X.then(Y).then(Z);
和:
$.when(X).then(Y).than(Z);
如果$.when
part是不必要的,为什么在jQuery中根本没有它呢?
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句