Home >Web Front-end >JS Tutorial >jquery Deferred 快速解决异步回调的问题

jquery Deferred 快速解决异步回调的问题

WBOY
WBOYOriginal
2016-06-01 09:54:121215browse

jquery Deferred 快速解决异步回调的问题

<code class="language-javascript">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){})</code>

//相关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

 

管道

<code class="language-javascript">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 );
});</code>

以上是本文章的全部内容,希望码农们能喜欢。 

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn