services/common/tests/unit/test_async_chain.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /* Any copyright is dedicated to the Public Domain.
     2  * http://creativecommons.org/publicdomain/zero/1.0/ */
     4 Cu.import("resource://services-common/async.js");
     6 function run_test() {
     7   _("Chain a few async methods, making sure the 'this' object is correct.");
     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;
    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 }

mercurial