function ok(name){
var
dfd =
new
$.Deferred();
callback:func(){
return
dfd.resolve( response );
}
return
dfd.promise();
}
$.when(ok(1),ok(2)).then(function(resp1,resp2){})
//相关API 分成3类
1类:$.when(pro1,pro1) 将多个 promise 对象以and的关系 合并为1个
2类:promise 激发为 解决 deferred.resolve([ args ] ) deferred.resolveWith( context, [ args ] )
和 拒绝 .reject .rejectWith
context 上下文 替换 this 和通知 .notify .notifyWith
3类: 对激发的响应 解决时deferred.done(args) 拒绝时 deferred.fail() 通知时 deferred.progress()
不管 解决 或 拒绝 deferred.always()
deferred.then( doneCallbacks, failCallbacks [, progressCallbacks] )
promise(或者叫deferred 延迟对象如何获取?)
var
dfd =
new
$.Deferred();
return dfd.promise();
返回promise当前状态
deferred.state() pending(尚未完成) resolved rejected
管道
deferred.pipe( [ doneFilter ], [ failFilter ] )
var defer = $.Deferred()
var filtered = defer.pipe( null, function( value ) {
return value * 3;
});
defer.reject( 6 );
filtered.fail(function( value ) {
alert( "Value is ( 3*6 = ) 18: " + value );
});