toolkit/modules/Promise.jsm

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
-rwxr-xr-x

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

     1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     2 /* vim: set ts=2 et sw=2 tw=80 filetype=javascript: */
     3 /* This Source Code Form is subject to the terms of the Mozilla Public
     4  * License, v. 2.0. If a copy of the MPL was not distributed with this
     5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     7 "use strict";
     9 this.EXPORTED_SYMBOLS = [
    10   "Promise"
    11 ];
    13 /**
    14  * This module implements the "promise" construct, according to the
    15  * "Promises/A+" proposal as known in April 2013, documented here:
    16  *
    17  * <http://promises-aplus.github.com/promises-spec/>
    18  *
    19  * A promise is an object representing a value that may not be available yet.
    20  * Internally, a promise can be in one of three states:
    21  *
    22  * - Pending, when the final value is not available yet.  This is the only state
    23  *   that may transition to one of the other two states.
    24  *
    25  * - Resolved, when and if the final value becomes available.  A resolution
    26  *   value becomes permanently associated with the promise.  This may be any
    27  *   value, including "undefined".
    28  *
    29  * - Rejected, if an error prevented the final value from being determined.  A
    30  *   rejection reason becomes permanently associated with the promise.  This may
    31  *   be any value, including "undefined", though it is generally an Error
    32  *   object, like in exception handling.
    33  *
    34  * A reference to an existing promise may be received by different means, for
    35  * example as the return value of a call into an asynchronous API.  In this
    36  * case, the state of the promise can be observed but not directly controlled.
    37  *
    38  * To observe the state of a promise, its "then" method must be used.  This
    39  * method registers callback functions that are called as soon as the promise is
    40  * either resolved or rejected.  The method returns a new promise, that in turn
    41  * is resolved or rejected depending on the state of the original promise and on
    42  * the behavior of the callbacks.  For example, unhandled exceptions in the
    43  * callbacks cause the new promise to be rejected, even if the original promise
    44  * is resolved.  See the documentation of the "then" method for details.
    45  *
    46  * Promises may also be created using the "Promise.defer" function, the main
    47  * entry point of this module.  The function, along with the new promise,
    48  * returns separate methods to change its state to be resolved or rejected.
    49  * See the documentation of the "Deferred" prototype for details.
    50  *
    51  * -----------------------------------------------------------------------------
    52  *
    53  * Cu.import("resource://gre/modules/Promise.jsm");
    54  *
    55  * // This function creates and returns a new promise.
    56  * function promiseValueAfterTimeout(aValue, aTimeout)
    57  * {
    58  *   let deferred = Promise.defer();
    59  *
    60  *   try {
    61  *     // An asynchronous operation will trigger the resolution of the promise.
    62  *     // In this example, we don't have a callback that triggers a rejection.
    63  *     do_timeout(aTimeout, function () {
    64  *       deferred.resolve(aValue);
    65  *     });
    66  *   } catch (ex) {
    67  *     // Generally, functions returning promises propagate exceptions through
    68  *     // the returned promise, though they may also choose to fail early.
    69  *     deferred.reject(ex);
    70  *   }
    71  *
    72  *   // We don't return the deferred to the caller, but only the contained
    73  *   // promise, so that the caller cannot accidentally change its state.
    74  *   return deferred.promise;
    75  * }
    76  *
    77  * // This code uses the promise returned be the function above.
    78  * let promise = promiseValueAfterTimeout("Value", 1000);
    79  *
    80  * let newPromise = promise.then(function onResolve(aValue) {
    81  *   do_print("Resolved with this value: " + aValue);
    82  * }, function onReject(aReason) {
    83  *   do_print("Rejected with this reason: " + aReason);
    84  * });
    85  *
    86  * // Unexpected errors should always be reported at the end of a promise chain.
    87  * newPromise.then(null, Components.utils.reportError);
    88  *
    89  * -----------------------------------------------------------------------------
    90  */
    92 // These constants must be defined on the "this" object for them to be visible
    93 // by subscripts in B2G, since "this" does not match the global scope.
    94 this.Cc = Components.classes;
    95 this.Ci = Components.interfaces;
    96 this.Cu = Components.utils;
    97 this.Cr = Components.results;
    99 this.Cc["@mozilla.org/moz/jssubscript-loader;1"]
   100     .getService(this.Ci.mozIJSSubScriptLoader)
   101     .loadSubScript("resource://gre/modules/Promise-backend.js", this);

mercurial