Method Properties
<static>
Deferred.call(fun, args)
//=> Deferred
Parameters
-
fun
: function(...[*]):*
- function to call
-
args
: ...*
- arguments passed to fun
`call` function is for calling function asynchronous.
// 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"]);
});
<static>
Deferred.chain(arguments)
//=> Deferred
Parameters
-
arguments
: ...[(Array.<function(*):*>|Object.<string|function(*):*>|function(*):*)]
- process chains
Construct Deferred chain with array and return its Deferred.
This is shorthand for construct Deferred chains.
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);
}
);
<static>
Deferred.connect(funo, options)
//=> function(...[*]):Deferred
Parameters
-
funo
: (function(...[*]):*|*)
- target function or object
-
options
: ({ok:number|ng:number|target:*}|string)
- options or method name of object in arguments[0]
Returns
function(...[*]):Deferred
Connect a function with Deferred. That is, transform a function
that takes a callback into one that returns a Deferred object.
var timeout = Deferred.connect(setTimeout, { target: window, ok: 0 });
timeout(1).next(function () {
alert('after 1 sec');
});
var timeout = Deferred.connect(window, "setTimeout");
timeout(1).next(function () {
alert('after 1 sec');
});
<static>
Deferred.define(obj, list)
//=> function():Deferred
Parameters
-
obj
: Object
-
list
: Array.<string>=
- (default Deferred.methods)
Returns
function():Deferred
The Deferred constructor function
<static>
Deferred.earlier(dl)
//=> Deferred
Parameters
-
dl
: (Array.<Deferred>|Object.<string|Deferred>)
- Deferreds wanted to wait
Continue process when one deferred in `deferredlist` has completed. Others will be canceled.
parallel ('and' processing) <=> earlier ('or' processing)
<static>
Deferred.isDeferred(obj)
//=> boolean
Parameters
-
obj
: *
- object to determine.
Returns true if an argument is Deferred.
<static>
Deferred.loop(n, fun)
//=> Deferred
Parameters
-
n
: (number|{begin:number|end:number|step:number})
- loop definition
-
fun
: function(number):*
- loop function
`loop` function provides browser-non-blocking loop.
This loop is slow but not stop browser's appearance.
This function waits a deferred returned by loop function.
//=> 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 with sleeping 1 sec in each loop.
loop(10, function (n) {
log(n);
return wait(1);
});
<static>
Deferred.next(fun)
//=> Deferred
Parameters
-
fun
: function():*
- callback
`next` is shorthand for creating new deferred which
is called after current queue.
<static>
Deferred.parallel(dl)
//=> Deferred
Parameters
-
dl
: (Array.<Deferred>|Object.<string|Deferred>)
- Deferreds wanted to wait
`parallel` wraps up `deferredlist` to one deferred.
This is useful when some asynchronous resources are required.
`deferredlist` can be Array or Object (Hash). If you specify
multiple objects as arguments, then they are wrapped into
an Array.
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
});
<static>
Deferred.register(name, fun)
Parameters
-
name
: string
- name of method
-
fun
: function(*):Deferred
- actual function of method
Register `fun` to Deferred prototype for method chain.
// Deferred.register("loop", loop);
// Global Deferred function
loop(10, function (n) {
print(n);
}).
// Registered Deferred.prototype.loop
loop(10, function (n) {
print(n);
});
<static>
Deferred.repeat(n, fun)
//=> Deferred
Parameters
-
n
: number
- loop count
-
fun
: function(number)
- loop function
Loop `n` times with `fun`.
This function automatically returns UI-control to browser, if the loop spends over 20msec.
This is useful for huge loop not to block browser UI.
This function can't wait a deferred returned by loop function, compared with Deferred.loop.
repeat(10, function (i) {
i //=> 0,1,2,3,4,5,6,7,8,9
});
<static>
Deferred.retry(retryCount, funcDeferred, options)
//=> Deferred
Parameters
-
retryCount
: number
-
funcDeferred
: function(number):Deferred
-
options
: {wait:number}
Try func (returns Deferred) till it finish without exceptions.
Deferred.retry(3, function () {
return http.get(...);
}).
next(function (res) {
res //=> response if succeeded
}).
error(function (e) {
e //=> error if all try failed
});
<static>
Deferred.wait(sec)
//=> Deferred
Parameters
-
sec
: number
- second to wait
`wait` returns deferred that will be called after `sec` elapsed
with real elapsed time (msec)
wait(1).next(function (elapsed) {
log(elapsed); //=> may be 990-1100
});
Deferred#call(val)
//=> Deferred
Parameters
-
val
: *
- Value passed to continuation.
Invokes self callback chain.
function timeout100 () {
var d = new Deferred();
setTimeout(function () {
d.call('value');
}, 100);
return d;
}
Deferred#cancel()
//=> Deferred
Cancel receiver callback (this is only valid before invoking any callbacks)
Deferred#error(fun)
//=> Deferred
Parameters
-
fun
: function(this:Deferred|...[*]):*
- Errorback of continuation.
Returns
Deferred
next 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.
var d = new Deferred();
d.
next(function () {
alert(1);
throw "foo";
}).
next(function () {
alert('not shown');
}).
error(function (e) {
alert(e); //=> "foo"
});
d.call();
Deferred#fail(val)
//=> Deferred
Parameters
-
val
: *
- Value of error.
Invokes self errorback chain. You can use this method for explicit errors (eg. HTTP request failed)
function http (url) {
var d = new Deferred();
var x = new XMLHttpRequest();
x.onreadystatechange = function () {
if (x.readyState == 4) {
if (x.status == 200) d.call(x); else d.fail(x);
}
};
return d;
}
Deferred#next(fun)
//=> Deferred
Parameters
-
fun
: function(this:Deferred|...[*]):*
- Callback of continuation.
Returns
Deferred
next deferred
Create new Deferred and sets `fun` as its callback.
var d = new Deferred();
d.
next(function () {
alert(1);
}).
next(function () {
alert(2);
});
d.call();