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