gecko/toolkit/modules/Promise.jsm
Jim Blandy 4d6a633bba Bug 914753: Make Emacs file variable header lines correct, or at least consistent. DONTBUILD r=ehsan
The -*- file variable lines -*- establish per-file settings that Emacs will
pick up. This patch makes the following changes to those lines (and touches
nothing else):

 - Never set the buffer's mode.

   Years ago, Emacs did not have a good JavaScript mode, so it made sense
   to use Java or C++ mode in .js files. However, Emacs has had js-mode for
   years now; it's perfectly serviceable, and is available and enabled by
   default in all major Emacs packagings.

   Selecting a mode in the -*- file variable line -*- is almost always the
   wrong thing to do anyway. It overrides Emacs's default choice, which is
   (now) reasonable; and even worse, it overrides settings the user might
   have made in their '.emacs' file for that file extension. It's only
   useful when there's something specific about that particular file that
   makes a particular mode appropriate.

 - Correctly propagate settings that establish the correct indentation
   level for this file: c-basic-offset and js2-basic-offset should be
   js-indent-level. Whatever value they're given should be preserved;
   different parts of our tree use different indentation styles.

 - We don't use tabs in Mozilla JS code. Always set indent-tabs-mode: nil.
   Remove tab-width: settings, at least in files that don't contain tab
   characters.

 - Remove js2-mode settings that belong in the user's .emacs file, like
   js2-skip-preprocessor-directives.
2014-06-24 22:12:07 -07:00

102 lines
4.3 KiB
JavaScript

/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set ts=2 et sw=2 tw=80 filetype=javascript: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
this.EXPORTED_SYMBOLS = [
"Promise"
];
/**
* This module implements the "promise" construct, according to the
* "Promises/A+" proposal as known in April 2013, documented here:
*
* <http://promises-aplus.github.com/promises-spec/>
*
* A promise is an object representing a value that may not be available yet.
* Internally, a promise can be in one of three states:
*
* - Pending, when the final value is not available yet. This is the only state
* that may transition to one of the other two states.
*
* - Resolved, when and if the final value becomes available. A resolution
* value becomes permanently associated with the promise. This may be any
* value, including "undefined".
*
* - Rejected, if an error prevented the final value from being determined. A
* rejection reason becomes permanently associated with the promise. This may
* be any value, including "undefined", though it is generally an Error
* object, like in exception handling.
*
* A reference to an existing promise may be received by different means, for
* example as the return value of a call into an asynchronous API. In this
* case, the state of the promise can be observed but not directly controlled.
*
* To observe the state of a promise, its "then" method must be used. This
* method registers callback functions that are called as soon as the promise is
* either resolved or rejected. The method returns a new promise, that in turn
* is resolved or rejected depending on the state of the original promise and on
* the behavior of the callbacks. For example, unhandled exceptions in the
* callbacks cause the new promise to be rejected, even if the original promise
* is resolved. See the documentation of the "then" method for details.
*
* Promises may also be created using the "Promise.defer" function, the main
* entry point of this module. The function, along with the new promise,
* returns separate methods to change its state to be resolved or rejected.
* See the documentation of the "Deferred" prototype for details.
*
* -----------------------------------------------------------------------------
*
* Cu.import("resource://gre/modules/Promise.jsm");
*
* // This function creates and returns a new promise.
* function promiseValueAfterTimeout(aValue, aTimeout)
* {
* let deferred = Promise.defer();
*
* try {
* // An asynchronous operation will trigger the resolution of the promise.
* // In this example, we don't have a callback that triggers a rejection.
* do_timeout(aTimeout, function () {
* deferred.resolve(aValue);
* });
* } catch (ex) {
* // Generally, functions returning promises propagate exceptions through
* // the returned promise, though they may also choose to fail early.
* deferred.reject(ex);
* }
*
* // We don't return the deferred to the caller, but only the contained
* // promise, so that the caller cannot accidentally change its state.
* return deferred.promise;
* }
*
* // This code uses the promise returned be the function above.
* let promise = promiseValueAfterTimeout("Value", 1000);
*
* let newPromise = promise.then(function onResolve(aValue) {
* do_print("Resolved with this value: " + aValue);
* }, function onReject(aReason) {
* do_print("Rejected with this reason: " + aReason);
* });
*
* // Unexpected errors should always be reported at the end of a promise chain.
* newPromise.then(null, Components.utils.reportError);
*
* -----------------------------------------------------------------------------
*/
// These constants must be defined on the "this" object for them to be visible
// by subscripts in B2G, since "this" does not match the global scope.
this.Cc = Components.classes;
this.Ci = Components.interfaces;
this.Cu = Components.utils;
this.Cr = Components.results;
this.Cc["@mozilla.org/moz/jssubscript-loader;1"]
.getService(this.Ci.mozIJSSubScriptLoader)
.loadSubScript("resource://gre/modules/Promise-backend.js", this);