Home > Web Front-end > JS Tutorial > jquery Deferred quickly solves the problem of asynchronous callback_jquery

jquery Deferred quickly solves the problem of asynchronous callback_jquery

WBOY
Release: 2016-05-16 15:06:38
Original
1191 people have browsed it

jquery Deferred quickly solves the problem of asynchronous callbacks

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){})
Copy after login

//Related APIs are divided into 3 categories

Category 1: $.when(pro1,pro1) merges multiple promise objects into one in the relationship of and

Category 2: promise is triggered to resolve deferred.resolve([ args ] ) deferred.resolveWith( context, [ args ] )

and reject .reject .rejectWith

context context replaces this and notification .notify .notifyWith

Category 3: Response to trigger deferred.done(args) when resolved, deferred.fail() when rejected, deferred.progress()

Regardless of resolution or rejection deferred.always()


deferred.then( doneCallbacks, failCallbacks [, progressCallbacks] )


Promise (or deferred, how to obtain deferred object?)

var dfd = new $.Deferred(); return dfd.promise();

Return the current status of promise

deferred.state() pending (not yet completed) resolved rejected

Pipeline

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 );
});
Copy after login

The above jquery Deferred quick solution to the problem of asynchronous callback is all the content shared by the editor. I hope it can give you a reference, and I hope you will support Script Home.

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