jquery Deferred 快速解决异步回调的问题
jquery Deferred 快速解决异步回调的问题
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 ); });
相关推荐
nmgxzm00 2020-11-10
xixixi 2020-11-11
88254251 2020-11-01
MarukoMa 2020-09-02
88234852 2020-09-15
陈旭阳 2020-08-31
whynotgonow 2020-08-19
前端开发Kingcean 2020-07-30
whynotgonow 2020-07-29
bowean 2020-07-08
前端开发Kingcean 2020-07-08
88520191 2020-07-05
前端开发Kingcean 2020-06-27
88481456 2020-06-18
whynotgonow 2020-06-16
88520191 2020-06-13
88520191 2020-06-13
89500297 2020-06-13