Latest web development tutorials

jQuery deferred.promise () method

jQuery Misc Methods jQuery Misc Methods

Examples

Set the delay time is a random two timers are used to resolve (resolve) and rejection (reject) Lingering objects

$ (Function () { function asyncEvent () { var dfd = new . jQuery Deferred (); // at a random time interval after Resolve (resolution state) setTimeout (function () { . dfd resolve ( "cheers");}, Math floor ( 400 + Math random () * 2000.));. // After a random time interval reject (Reject State) setTimeout (function () { . dfd reject ( "Sorry");.}, Math floor (400 + Math random () * 2000.)); // display every half second a "working ..." message setTimeout (function working () { if ( dfd. state () === "pending " ) { . dfd notify ( "working ..." ); setTimeout (working, 500);} }, 1); // return Promise objects, the caller can not change the lingering objects return . dfd promise ();} // Asynchronous function attach a done, fail, and progress handlers $. When ( asyncEvent () ). Then ( function (status) { alert ( status + ', things are going well' );}, Function (status) { alert ( status + ', this time you have failed' );}, Function (status) {$ ( "body") append (status);.} );})

try it"

Definition and Usage

deferred.promise () function returns the Deferred (delay) the Promise object.

Note: 1. The method allows an asynchronous function that prevents progress to interfere in its internal requests (progress) or state (status) of the other code.
2. Only a group comprising deferred object, comprising: done (), then (), fail (), isResolved (), isRejected (), always (), these methods can only observe a deferred state, and can not be changed deferred internal state of the object.
3. deferred.promise () can accept a target parameter, then the incoming target will be given Promise method, and returned as the result, rather than creating a new object.


grammar

deferred.promise( [target ] )

参数 描述
target Object类型 绑定 promise 方法的对象。


Examples

More examples

Use target parameters
Use the target parameter, promote existing objects Promise


jQuery Misc Methods jQuery Misc Methods