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.
$.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 function is constructor of Deferred.
Sample:
var d = new Deferred(); // or this is shothand of above. var d = Deferred();
Create new Deferred and sets fun as its callback.
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.
Invokes self callback chain.
Invokes self errorback chain.
Cancels self callback chain.
next is shorthand for creating new deferred which
is called after current queue.
Construct Deferred chain with array and return its Deferred. This is shorthand for construct Deferred chains.
Sample:
return chain(
function () {
return wait(0.5);
},
function (w) {
throw "foo";
},
function error (e) {
alert(e);
},
[
function () {
return wait(1);
},
function () {
return wait(2);
}
],
function (result) {
alert([ result[0], result[1] ]);
},
{
foo: wait(1),
bar: wait(1)
},
function (result) {
alert([ result.foo, result.bar ]);
},
function error (e) {
alert(e);
}
);
wait returns deferred that will be called after sec elapsed
with real elapsed time (msec)
Sample:
wait(1).next(function (elapsed) {
log(elapsed); //=> may be 990-1100
});
call function is for calling function asynchronous.
Sample:
// like tail recursion
next(function () {
function pow (x, n) {
function _pow (n, r) {
print([n, r]);
if (n == 0) return r;
return call(_pow, n - 1, x * r);
}
return call(_pow, n, 1);
}
return call(pow, 2, 10);
}).
next(function (r) {
print([r, "end"]);
});
parallel wraps up deferredlist to one deferred.
This is useful when some asynchronous resources required.
deferredlist can be Array or Object (Hash). If you specify multiple objects as arguments, then they are wrapped into an Array.
Sample:
parallel([
$.get("foo.html"),
$.get("bar.html")
]).next(function (values) {
values[0] //=> foo.html data
values[1] //=> bar.html data
});
parallel({
foo: $.get("foo.html"),
bar: $.get("bar.html")
}).next(function (values) {
values.foo //=> foo.html data
values.bar //=> bar.html data
});
Continue process when one deferred in deferredlist has completed. Others will cancel. parallel ('and' processing) <=> earlier ('or' processing)
loop function provides browser-non-blocking loop.
This loop is slow but not stop browser's appearance.
Sample:
//=> loop 1 to 100
loop({begin:1, end:100, step:10}, function (n, o) {
for (var i = 0; i < o.step; i++) {
log(n+i);
}
});
//=> loop 10 times
loop(10, function (n) {
log(n);
});
Loop n tiems with fun. This function automatically return control to browser, if loop time over 20msec. This is useful for huge loop not to block browser UI.
Sample::
repeat(10, function (i) {
i //=> 0,1,2,3,4,5,6,7,8,9
});
Register fun to Deferred prototype for method chain.
Sample::
// Deferred.register("loop", loop);
// Global Deferred function
loop(10, function (n) {
print(n);
}).
// Registered Deferred.prototype.loop
loop(10, function (n) {
print(n);
});