Home > Web Front-end > JS Tutorial > body text

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

WBOY
Release: 2016-06-01 09:54:12
Original
1188 people have browsed it

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>
Copy after login

//相关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>
Copy after login

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template