|
1 |
| -jquery-async-plugin |
| 1 | +jQuery async plugin |
2 | 2 | ===================
|
3 | 3 |
|
4 |
| -jQuery async plugin add Deferred to handle like the Mochikit.Async.Deferred. |
| 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). |
| 5 | + |
| 6 | +## Installation |
| 7 | + |
| 8 | +Include script after the jQuery library: |
| 9 | + |
| 10 | + ```html |
| 11 | + <script src="/path/to/jquery.async.js"></script> |
| 12 | + ``` |
| 13 | + |
| 14 | +This plugin adds **async** function to jQuery object. |
| 15 | + |
| 16 | +$.async() is a shortcut function faster way of creating new Deferred sequence. |
| 17 | + |
| 18 | + ```javascript |
| 19 | + $.async(function() { |
| 20 | + console.log('Start Deferred chain'); |
| 21 | + }).addCallback(function() { |
| 22 | + console.log('End Deferred chain'); |
| 23 | + }); |
| 24 | + ``` |
| 25 | + |
| 26 | +## Usage |
| 27 | + |
| 28 | +The basic usage is the same as the Mochikit.Async.Deferred. |
| 29 | + |
| 30 | + |
| 31 | +Simple Deferred chain: |
| 32 | + |
| 33 | + ```javascript |
| 34 | + var d = $.Deferred(); |
| 35 | + d.addCallback(function() { |
| 36 | + return 1; |
| 37 | + }).addCallback(function(res) { |
| 38 | + console.log(res); // 1 |
| 39 | + }); |
| 40 | + d.callback(); |
| 41 | + ``` |
| 42 | + |
| 43 | +Using [succeed](http://mochi.github.io/mochikit/doc/html/MochiKit/Async.html#fn-succeed)(): |
| 44 | + |
| 45 | + ```javascript |
| 46 | + $.async.succeed(1).addCallback(function(res) { |
| 47 | + return res + 1; |
| 48 | + }).addCallback(function(res) { |
| 49 | + console.log(res); // 2 |
| 50 | + }); |
| 51 | + |
| 52 | + |
| 53 | +Passing Values and Error handling: |
| 54 | + |
| 55 | + ```javascript |
| 56 | + $.async(function() { |
| 57 | + return 1; |
| 58 | + }).addCallback(function(res) { |
| 59 | + console.log(res); // 1 |
| 60 | + throw new Error('error'); |
| 61 | + }).addCallback(function(res) { |
| 62 | + console.log('This message does not show'); |
| 63 | + return 'noop'; |
| 64 | + }).addErrback(function(err) { |
| 65 | + console.log(err); // error |
| 66 | + return 'hello'; |
| 67 | + }).addBoth(function(res) { |
| 68 | + console.log(res); // hello |
| 69 | + }).callback(); |
| 70 | + ``` |
| 71 | + |
| 72 | +## License |
| 73 | + |
| 74 | +Licensed under the MIT license. |
| 75 | + |
| 76 | +## Authors |
| 77 | + |
| 78 | +* [polygon planet](https://github.com/polygonplanet) (twitter: [polygon_planet](http://twitter.com/polygon_planet)) |
| 79 | + |
| 80 | + |
| 81 | + |
0 commit comments