test_async_chain.js (837B)
1 /* Any copyright is dedicated to the Public Domain. 2 * http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 const { Async } = ChromeUtils.importESModule( 5 "resource://services-common/async.sys.mjs" 6 ); 7 8 function run_test() { 9 _("Chain a few async methods, making sure the 'this' object is correct."); 10 11 let methods = { 12 save(x, callback) { 13 this.x = x; 14 callback(x); 15 }, 16 addX(x, callback) { 17 callback(x + this.x); 18 }, 19 double(x, callback) { 20 callback(x * 2); 21 }, 22 neg(x, callback) { 23 callback(-x); 24 }, 25 }; 26 methods.chain = Async.chain; 27 28 // ((1 + 1 + 1) * (-1) + 1) * 2 + 1 = -3 29 methods.chain( 30 methods.save, 31 methods.addX, 32 methods.addX, 33 methods.neg, 34 methods.addX, 35 methods.double, 36 methods.addX, 37 methods.save 38 )(1); 39 Assert.equal(methods.x, -3); 40 }