|
1 /* Any copyright is dedicated to the Public Domain. |
|
2 * http://creativecommons.org/publicdomain/zero/1.0/ */ |
|
3 |
|
4 Cu.import("resource://services-common/async.js"); |
|
5 |
|
6 function run_test() { |
|
7 _("Chain a few async methods, making sure the 'this' object is correct."); |
|
8 |
|
9 let methods = { |
|
10 save: function(x, callback) { |
|
11 this.x = x; |
|
12 callback(x); |
|
13 }, |
|
14 addX: function(x, callback) { |
|
15 callback(x + this.x); |
|
16 }, |
|
17 double: function(x, callback) { |
|
18 callback(x * 2); |
|
19 }, |
|
20 neg: function(x, callback) { |
|
21 callback(-x); |
|
22 } |
|
23 }; |
|
24 methods.chain = Async.chain; |
|
25 |
|
26 // ((1 + 1 + 1) * (-1) + 1) * 2 + 1 = -3 |
|
27 methods.chain(methods.save, methods.addX, methods.addX, methods.neg, |
|
28 methods.addX, methods.double, methods.addX, methods.save)(1); |
|
29 do_check_eq(methods.x, -3); |
|
30 } |