Bug 1131829 - remove PromiseUtils.resolveOrTimeout. r=yoric
authorMark Hammond <mhammond@skippinet.com.au>
Fri, 13 Feb 2015 08:43:31 +1100
changeset 228948 463ff2bdf8fb55ed80b38d5989945b40169f1e1c
parent 228899 2f5c5ec1a24b9da27ee21e737239289f0f7105ec
child 228949 7fd767f1b043ecb5a249c85b78260f352cb69833
push id28275
push userryanvm@gmail.com
push dateFri, 13 Feb 2015 17:13:18 +0000
treeherdermozilla-central@aa339e258ccd [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersyoric
bugs1131829
milestone38.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1131829 - remove PromiseUtils.resolveOrTimeout. r=yoric
toolkit/modules/PromiseUtils.jsm
toolkit/modules/tests/xpcshell/test_PromiseUtils.js
--- a/toolkit/modules/PromiseUtils.jsm
+++ b/toolkit/modules/PromiseUtils.jsm
@@ -5,60 +5,16 @@
 "use strict"
 
 this.EXPORTED_SYMBOLS = ["PromiseUtils"];
 
 Components.utils.import("resource://gre/modules/Timer.jsm");
 
 this.PromiseUtils = {
   /*
-   * A simple timeout mechanism.
-   *
-   * Example:
-   * resolveOrTimeout(myModule.shutdown(), 1000,
-   *                  () => new Error("The module took too long to shutdown"));
-   *
-   * @param {Promise} promise The Promise that should resolve/reject quickly.
-   * @param {number} delay A delay after which to stop waiting for `promise`, in milliseconds.
-   * @param {function} rejection If `promise` hasn't resolved/rejected after `delay`,
-   * a value used to construct the rejection.
-   *
-   * @return {Promise} A promise that behaves as `promise`, if `promise` is
-   * resolved/rejected within `delay` ms, or rejects with `rejection()` otherwise.
-   */
-  resolveOrTimeout : function(promise, delay, rejection)  {
-    // throw a TypeError if <promise> is not a Promise object
-    if (!(promise instanceof Promise)) {
-      throw new TypeError("first argument <promise> must be a Promise object");
-    }
-
-    // throw a TypeError if <delay> is not a number
-    if (typeof delay != "number" || delay < 0) {
-      throw new TypeError("second argument <delay> must be a positive number");
-    }
-
-    // throws a TypeError if <rejection> is not a function
-    if (rejection && typeof rejection != "function") {
-      throw new TypeError("third optional argument <rejection> must be a function");
-    }
-
-    return new Promise((resolve, reject) => {
-      promise.then(resolve, reject);
-      let id = setTimeout(() => {
-        try {
-          rejection ? reject(rejection()) : reject(new Error("Promise Timeout"));
-        } catch(ex) {
-          reject(ex);
-        }
-        clearTimeout(id);
-      }, delay);
-    });
-  },
-
-  /*
    * Creates a new pending Promise and provide methods to resolve and reject this Promise.
    *
    * @return {Deferred} an object consisting of a pending Promise "promise"
    * and methods "resolve" and "reject" to change its state.
    */
   defer : function() {
     return new Deferred();
   },
--- a/toolkit/modules/tests/xpcshell/test_PromiseUtils.js
+++ b/toolkit/modules/tests/xpcshell/test_PromiseUtils.js
@@ -6,86 +6,16 @@
 Components.utils.import("resource://gre/modules/PromiseUtils.jsm");
 Components.utils.import("resource://gre/modules/Timer.jsm");
 
 // Tests for PromiseUtils.jsm
 function run_test() {
   run_next_test();
 }
 
-///////////////////////////////////////////////////////////////////////////////////////////
-// Tests for PromiseUtils.resolveOrTimeout()
-///////////////////////////////////////////////////////////////////////////////////////////
-
-/* Tests for the case when arguments to resolveOrTimeout
- * are not of correct type */
-add_task(function* test_wrong_arguments() {
-  let p = new Promise((resolve, reject) => {});
-  // for the first argument
-  Assert.throws(() => PromiseUtils.resolveOrTimeout("string", 200), /first argument <promise> must be a Promise object/,
-                "TypeError thrown because first argument is not a Promsie object");
-  // for second argument
-  Assert.throws(() => PromiseUtils.resolveOrTimeout(p, "string"), /second argument <delay> must be a positive number/,
-                "TypeError thrown because second argument is not a positive number");
-  // for the third argument
-  Assert.throws(() => PromiseUtils.resolveOrTimeout(p, 200, "string"), /third optional argument <rejection> must be a function/,
-                "TypeError thrown because thrird argument is not a function");
-});
-
-/* Tests for the case when the optional third argument is not provided
- * In that case the returned promise rejects with a default Error */
-add_task(function* test_optional_third_argument() {
-  let p = new Promise((resolve, reject) => {});
-  yield Assert.rejects(PromiseUtils.resolveOrTimeout(p, 200), /Promise Timeout/, "Promise rejects with a default Error");
-});
-
-/* Test for the case when the passed promise resolves quickly
- * In that case the returned promise also resolves with the same value */
-add_task(function* test_resolve_quickly() {
-  let p = new Promise((resolve, reject) => setTimeout(() => resolve("Promise is resolved"), 20));
-  let result = yield PromiseUtils.resolveOrTimeout(p, 200);
-  Assert.equal(result, "Promise is resolved", "Promise resolves quickly");
-});
-
-/* Test for the case when the passed promise rejects quickly
- * In that case the returned promise also rejects with the same value */
-add_task(function* test_reject_quickly() {
-  let p = new Promise((resolve, reject) => setTimeout(() => reject("Promise is rejected"), 20));
-  yield Assert.rejects(PromiseUtils.resolveOrTimeout(p, 200), /Promise is rejected/, "Promise rejects quickly");
-});
-
-/* Tests for the case when the passed promise doesn't settle
- * and rejection returns string/object/undefined */
-add_task(function* test_rejection_function() {
-  let p = new Promise((resolve, reject) => {});
-  // for rejection returning string
-  yield Assert.rejects(PromiseUtils.resolveOrTimeout(p, 200, () => {
-    return "Rejection returned a string";
-  }), /Rejection returned a string/, "Rejection returned a string");
-
-  // for rejection returning object
-  yield Assert.rejects(PromiseUtils.resolveOrTimeout(p, 200, () => {
-    return {Name:"Promise"};
-  }), Object, "Rejection returned an object");
-
-  // for rejection returning undefined
-  yield Assert.rejects(PromiseUtils.resolveOrTimeout(p, 200, () => {
-    return;
-  }), undefined, "Rejection returned undefined");
-});
-
-/* Tests for the case when the passed promise doesn't settles
- * and rejection throws an error */
-add_task(function* test_rejection_throw_error() {
-  let p = new Promise((resolve, reject) => {});
-  yield Assert.rejects(PromiseUtils.resolveOrTimeout(p, 200, () => {
-    throw new Error("Rejection threw an Error");
-  }), /Rejection threw an Error/, "Rejection threw an error");
-});
-
 ///////////////////////////////////////////////////////////////////////////////////////
 // Tests for PromiseUtils.defer()
 ///////////////////////////////////////////////////////////////////////////////////////
 
 /* Tests for checking the resolve method of the Deferred object
  * returned by PromiseUtils.defer() */
 add_task(function* test_resolve_string() {
   let def = PromiseUtils.defer();