Skip to content

Commit c15fce9

Browse files
committed
update README
1 parent 35e4d95 commit c15fce9

File tree

1 file changed

+182
-3
lines changed

1 file changed

+182
-3
lines changed

README.md

Lines changed: 182 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
jQuery async plugin
22
===================
33

4-
jQuery async plugin add **Deferred** to handle like the [Mochikit.Async.Deferred](http://mochi.github.io/mochikit/doc/html/MochiKit/Async.html#fn-deferred).
4+
jQuery async plugin adds **Deferred** to handle like the [Mochikit.Async.Deferred](http://mochi.github.io/mochikit/doc/html/MochiKit/Async.html#fn-deferred).
5+
6+
This plugin adds the Deferred functions to jQuery.Deferred object, but it does not conflict with other plugins.
7+
jQuery.Deferred keeps original functions.
8+
jQuery object is added only "**async**" function object.
9+
510

611
## Installation
712

@@ -11,8 +16,6 @@ Include script after the jQuery library:
1116
<script src="/path/to/jquery.async.js"></script>
1217
```
1318

14-
This plugin adds **async** function to jQuery object.
15-
1619
$.async() is a shortcut function faster way of creating new Deferred sequence.
1720

1821
```javascript
@@ -69,6 +72,182 @@ $.async(function() {
6972
});
7073
```
7174

75+
## Function reference
76+
77+
*Portions of this document are reference from MochiKit.*
78+
79+
### $.Deferred() *Deferred*
80+
81+
The Deferred object usage is the same as the jQuery.Deferred.
82+
83+
```javascript
84+
var d = $.Deferred();
85+
```
86+
87+
The following callback methods has been added.
88+
89+
#### [addCallback](http://mochi.github.io/mochikit/doc/html/MochiKit/Async.html#fn-deferred.prototype.addcallback)(*function* callback) *Deferred*
90+
91+
Add a single callback to the end of the callback sequence.
92+
93+
#### [addErrback](http://mochi.github.io/mochikit/doc/html/MochiKit/Async.html#fn-deferred.prototype.adderrback)(*function* errback) *Deferred*
94+
95+
Add a single errback to the end of the callback sequence.
96+
97+
#### [addBoth](http://mochi.github.io/mochikit/doc/html/MochiKit/Async.html#fn-deferred.prototype.addboth)(*function* func) *Deferred*
98+
99+
Add the same function as both a callback and an errback as the next element on the callback sequence. This is useful for code that you want to guarantee to run.
100+
101+
#### [addCallbacks](http://mochi.github.io/mochikit/doc/html/MochiKit/Async.html#fn-deferred.prototype.addcallbacks)(*function* callback, *function* errback) *Deferred*
102+
103+
Add separate callback and errback to the end of the callback sequence. Either callback or errback may be null, but not both.
104+
105+
#### [callback](http://mochi.github.io/mochikit/doc/html/MochiKit/Async.html#fn-deferred.prototype.callback)([*** result]) *Deferred*
106+
107+
Begin the callback sequence with a non-Error result. Result may be any value except for a Deferred.
108+
109+
#### [errback](http://mochi.github.io/mochikit/doc/html/MochiKit/Async.html#fn-deferred.prototype.errback)([*** result]) *Deferred*
110+
111+
Begin the callback sequence with an error result. Result may be any value except for a Deferred.
112+
113+
#### [cancel](http://mochi.github.io/mochikit/doc/html/MochiKit/Async.html#fn-deferred.prototype.cancel)() *Deferred*
114+
115+
Cancels a Deferred that has not yet received a value, or is waiting on another Deferred as its value.
116+
117+
```javascript
118+
var d = $.Deferred();
119+
d.addCallback() {
120+
return 1;
121+
}).addCallback(res) {
122+
console.log(res); // 1
123+
throw 'ExampleError';
124+
}).addCallback(function() {
125+
neverHappen();
126+
}).addErrback(function(err) {
127+
console.log(err); // ExampleError
128+
}).addCallback(function() {
129+
if (Math.random() * 10 > 5) {
130+
throw 'RandomError';
131+
}
132+
return 'random test';
133+
}).addBoth(function(res) {
134+
console.log(res); // RandomError or 'random test'
135+
});
136+
// fire chain
137+
d.callback();
138+
```
139+
140+
### $.async(*function* func) *Deferred*
141+
142+
A shortcut faster way of creating new Deferred sequence.
143+
144+
```javascript
145+
$.async(function() {
146+
console.log('Start Deferred chain');
147+
}).addCallback(function() {
148+
console.log('End Deferred chain');
149+
});
150+
```
151+
152+
### $.async.[succeed](http://mochi.github.io/mochikit/doc/html/MochiKit/Async.html#fn-succeed)([*** result]) *Deferred*
153+
154+
Return a Deferred that has already had .callback(result) called.
155+
156+
```javascript
157+
$.async.succeed(1).addCallback(function(res) {
158+
console.log(res); // 1
159+
});
160+
```
161+
162+
### $.async.[fail](http://mochi.github.io/mochikit/doc/html/MochiKit/Async.html#fn-fail)([*** result]) *Deferred*
163+
164+
Return a Deferred that has already had .errback(result) called.
165+
166+
```javascript
167+
$.async.fail(1).addErrback(function(err) {
168+
console.log(err); // Error: 1
169+
});
170+
```
171+
172+
### $.async.[maybeDeferred](http://mochi.github.io/mochikit/doc/html/MochiKit/Async.html#fn-maybedeferred)(*** func) *Deferred*
173+
174+
Call a func with the given arguments and ensure the result is a Deferred.
175+
176+
```javascript
177+
var d = $.async.succeed(1);
178+
var s = 'abc';
179+
var random = (Math.random() * 10 < 5);
180+
$.async.maybeDeferred( random ? d : s ).addCallback(function(res) {
181+
console.log(res); // 1 or 'abc'
182+
});
183+
```
184+
185+
### $.async.maybeDeferreds(*** ...args) *Array*
186+
187+
Return an array of Deferred instances.
188+
189+
```javascript
190+
var list = $.async.maybeDeferreds(
191+
1, 2, 'foo', 'bar',
192+
function() { return 5 },
193+
$.async.succeed(100)
194+
);
195+
console.log(list); // [ 1, 2, ... (deferred instances) ]
196+
list[0].addCallback(function(res) {
197+
console.log(res); // 1
198+
});
199+
```
200+
201+
### $.async.[wait](http://mochi.github.io/mochikit/doc/html/MochiKit/Async.html#fn-wait)(*Number* seconds[, *** res]) *Deferred*
202+
203+
Return a new cancellable Deferred that will .callback(res) after at least seconds seconds have elapsed.
204+
205+
```javascript
206+
// Called after 5 seconds.
207+
$.async.wait(5).addCallback(function() {
208+
console.log('Begin wait() test');
209+
}).addCallback(function() {
210+
return $.async.wait(2); // Wait 2 seconds.
211+
}).addCallback(function() {
212+
console.log('End wait() test');
213+
});
214+
```
215+
216+
### $.async.[callLater](http://mochi.github.io/mochikit/doc/html/MochiKit/Async.html#fn-calllater)(*Number* seconds, *funcion* func[, *** args...]) *Deferred*
217+
218+
Call func(args...) after at least seconds seconds have elapsed.
219+
220+
```javascript
221+
var value = null;
222+
// Called after 1 second.
223+
$.async.callLater(1, function() {
224+
value = 'hoge';
225+
});
226+
console.log(value); // null
227+
$.async.callLater(1, function() {
228+
console.log(value); // 'hoge'
229+
});
230+
```
231+
232+
### $.async.till(*function* cond) *Deferred*
233+
234+
Wait until the condition completed. If true returned, waiting state will end.
235+
236+
```javascript
237+
console.log('Begin till');
238+
$.async.till(function() {
239+
// wait until the DOM body element is loaded
240+
if (!document.body) {
241+
return false;
242+
} else {
243+
return true;
244+
}
245+
}).addCallback(function() {
246+
console.log('End till');
247+
document.body.innerHTML += 'Hello';
248+
});
249+
```
250+
72251
## License
73252

74253
Licensed under the MIT license.

0 commit comments

Comments
 (0)