Home >Web Front-end >JS Tutorial >jquery Deferred quickly solves the problem of asynchronous callback_jquery
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){})
//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 ); });
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.