JSDeferred

Header::

JSDeferred Copyright (c) 2007 cho45 ( www.lowreal.net )

http://coderepos.org/share/wiki/JSDeferred

Version:: 0.3.1 License:: MIT

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Usage (with jQuery)::

$.deferred.define();

$.get("/hoge").next(function (data) {
    alert(data);
}).

parallel([$.get("foo.html"), $.get("bar.html")]).next(function (values) {
    log($.map(values, function (v) { return v.length }));
    if (values[1].match(/nextUrl:\s*(\S+)/)) {
        return $.get(RegExp.$1).next(function (d) {
            return d;
        });
    }
}).
next(function (d) {
    log(d.length);
});

 

Deferred ( ) //=> constructor

Deferred function is constructor of Deferred.

Sample:

var d = new Deferred();
// or this is shothand of above.
var d = Deferred();
 

Deferred.prototype.next ( fun ) //=> Deferred

Create new Deferred and sets fun as its callback.

Deferred.prototype.error ( fun ) //=> Deferred

Create new Deferred and sets fun as its errorback.

If fun not throws error but returns normal value, Deferred treats the given error is recovery and continue callback chain.

Deferred.prototype.call ( val ) //=> this

Invokes self callback chain.

Deferred.prototype.fail ( err ) //=> this

Invokes self errorback chain.

Deferred.prototype.cancel ( err ) //=> this

Cancels self callback chain.