JQuery defered对象的个人理解

关于Defered对象,主要包含以下一些常用的方法:

methods version args description
deferred.always(alwaysCallbacks [, alwaysCallbacks]) 1.6 A function, or array of functions, that is called when the Deferred is resolved or rejected. Add handlers to be called when the Deferred object is either resolved or rejected.
deferred.done(doneCallbacks [, doneCallbacks ]) 1.5 A function, or array of functions, that are called when the Deferred is resolved. Add handlers to be called when the Deferred object is resolved.
deferred.fail( failCallbacks [, failCallbacks ]) 1.5 A function, or array of functions, that are called when the Deferred is rejected. Add handlers to be called when the Deferred object is rejected.
deferred.then(doneFilter [, failFilter ] [, progressFilter ] ) 1.8 doneFilter: A function that is called when the Deferred is resolved.
failFilter: An optional function that is called when the Deferred is rejected.
progressFilter: An optional function that is called when progress notifications are sent to the Deferred.
Add handlers to be called when the Deferred object is resolved, rejected, or still in progress.
deferred.catch(failFilter ) 3.0 A function that is called when the Deferred is rejected. Add handlers to be called when the Deferred object is rejected.
deferred.progress() 1.7 A function, or array of functions, to be called when the Deferred generates progress notifications. Add handlers to be called when the Deferred object generates progress notifications.
deferred.notify(args) 1.7 Optional arguments that are passed to the progressCallbacks. Call the progressCallbacks on a Deferred object with the given args.
deferred.notifyWith( context [, args ] ) 1.7 context: Context passed to the progressCallbacks as the this object.
args: An optional array of arguments that are passed to the progressCallbacks.
Call the progressCallbacks on a Deferred object with the given context and args.
deferred.reject([args ] ) 1.5 Optional arguments that are passed to the failCallbacks. Reject a Deferred object and call any failCallbacks with the given args.
deferred.rejectWith( context [, args ]) 1.5 context: Context passed to the failCallbacks as the this object.
args: An optional array of arguments that are passed to the failCallbacks.
Reject a Deferred object and call any failCallbacks with the given context and args.
deferred.resolve([args ] ) 1.5 Optional arguments that are passed to the doneCallbacks. Resolve a Deferred object and call any doneCallbacks with the given args.
deferred.resolveWith(context [, args ] ) 1.5 context: Context passed to the doneCallbacks as the this object.
args: An optional array of arguments that are passed to the doneCallbacks.
Resolve a Deferred object and call any doneCallbacks with the given context and args.
deferred.state() 1.7 This method does not accept any arguments. Determine the current state of a Deferred object. Returns: “pending” or “resolved” or “rejected”
”pending”: The Deferred object is not yet in a completed state (neither “rejected” nor “resolved”).
deferred.promise([target ] ) 1.5 Object onto which the promise methods have to be attached Return a Deferred’s Promise object.

其他详细信息请参照JQuery Defered 官网

如下是一个简单的例子:

$(function () {
    var def = $.Deferred();
    def.always(function (...val) {
            console.log(this);
            console.log(val);
            console.log("always: " + val + " status: " + def.state());
        })
        .done(function (val) {
            console.log("done: " + val + " status: " + def.state());
        })
        .then(function (val) {
            console.log("then: " + val + " status: " + def.state());
        })
        .pipe(function (pipe) {
            console.log("pipe: " + pipe + " status: " + def.state());
        })
        .fail(function (err) {
            console.log("fail: " + err + " status: " + def.state());
        })
        .catch(function (e) {
            console.log("catch: " + e + " status: " + def.state());
        })
        .progress(function (p) {
            console.log("progress> " + p);
        });
    def.notify('notify').notify('notify1').resolveWith({a:1, b:2}, [3,4,5,6,7]);
    // def.notify('notify').notify('notify1').reject('reject');
});
//{a: 1, b: 2}
//(5) [3, 4, 5, 6, 7]
//always: 3,4,5,6,7 status: resolved
//done: 3 status: resolved
//pipe: 3 status: resolved
//then: undefined status: resolved
//progress> notify
//progress> notify1

注意:

  • resolveWith的第一个参数是always | done 函数的 this,第二个参数是一个数组,always | done 函数需要使用(…val)方式去接收。
  • notify可以调用多次,都会被progress所接收
  • done函数执行后,若是后面还有其他的done | then 函数时,参数不为空,但若是中间跟有其他fail | pipe | progress | catch 函数时,参数为undefined。

你可能感兴趣的:(JQuery)