Bug 1366531 - convert uses of defer to 'new Promise' in client/jsonview draft
authorOriol Brufau <oriol-bugzilla@hotmail.com>
Thu, 09 Nov 2017 00:07:44 +0100
changeset 695616 5ebea0ddb9c7bd9bc3161eb473114182b6d3fdc2
parent 694565 40df5dd35fdb7ce3652fe4448ac8961c075c928e
child 739653 21ebbb6cdf7652226bdd5da474cf3e8215ff8cdd
push id88480
push userbmo:oriol-bugzilla@hotmail.com
push dateThu, 09 Nov 2017 14:57:52 +0000
bugs1366531
milestone58.0a1
Bug 1366531 - convert uses of defer to 'new Promise' in client/jsonview MozReview-Commit-ID: InGMTAQ0X2R
devtools/client/jsonview/test/head.js
--- a/devtools/client/jsonview/test/head.js
+++ b/devtools/client/jsonview/test/head.js
@@ -27,64 +27,57 @@ registerCleanupFunction(() => {
  * @param {String} url
  *   The url to be loaded in the new tab.
  * @param {Number} timeout [optional]
  *   The maximum number of milliseconds allowed before the initialization of the
  *   JSON Viewer once the tab has been loaded. If exceeded, the initialization
  *   will be considered to have failed, and the returned promise will be rejected.
  *   If this parameter is not passed or is negative, it will be ignored.
  */
-function addJsonViewTab(url, timeout = -1) {
+async function addJsonViewTab(url, timeout = -1) {
   info("Adding a new JSON tab with URL: '" + url + "'");
 
-  let deferred = defer();
-  addTab(url).then(tab => {
-    let browser = tab.linkedBrowser;
+  let tab = await addTab(url);
+  let browser = tab.linkedBrowser;
 
-    // Load devtools/shared/frame-script-utils.js
-    getFrameScript();
+  // Load devtools/shared/frame-script-utils.js
+  getFrameScript();
 
-    // Load frame script with helpers for JSON View tests.
-    let rootDir = getRootDirectory(gTestPath);
-    let frameScriptUrl = rootDir + "doc_frame_script.js";
-    browser.messageManager.loadFrameScript(frameScriptUrl, false);
+  // Load frame script with helpers for JSON View tests.
+  let rootDir = getRootDirectory(gTestPath);
+  let frameScriptUrl = rootDir + "doc_frame_script.js";
+  browser.messageManager.loadFrameScript(frameScriptUrl, false);
 
-    // Check if there is a JSONView object.
-    if (!content.window.wrappedJSObject.JSONView) {
-      deferred.reject("JSON Viewer did not load.");
-      return;
-    }
+  // Check if there is a JSONView object.
+  if (!content.window.wrappedJSObject.JSONView) {
+    throw new Error("JSON Viewer did not load.");
+  }
 
-    // Resolve if the JSONView is fully loaded or wait
-    // for an initialization event.
-    if (content.window.wrappedJSObject.JSONView.initialized) {
-      deferred.resolve(tab);
-    } else {
-      waitForContentMessage("Test:JsonView:JSONViewInitialized").then(() => {
-        deferred.resolve(tab);
-      });
-    }
+  // Resolve if the JSONView is fully loaded.
+  if (content.window.wrappedJSObject.JSONView.initialized) {
+    return tab;
+  }
+
+  // Otherwise wait for an initialization event, possibly with a time limit.
+  const onJSONViewInitialized =
+    waitForContentMessage("Test:JsonView:JSONViewInitialized")
+    .then(() => tab);
 
-    // Add a timeout.
-    if (timeout >= 0) {
-      new Promise(resolve => {
-        if (content.window.document.readyState === "complete") {
-          resolve();
-        } else {
-          waitForContentMessage("Test:JsonView:load").then(resolve);
-        }
-      }).then(() => {
-        setTimeout(() => {
-          deferred.reject("JSON Viewer did not load.");
-        }, timeout);
-      });
-    }
-  });
+  if (!(timeout >= 0)) {
+    return onJSONViewInitialized;
+  }
 
-  return deferred.promise;
+  if (content.window.document.readyState !== "complete") {
+    await waitForContentMessage("Test:JsonView:load");
+  }
+
+  let onTimeout = new Promise((_, reject) =>
+    setTimeout(() => reject(new Error("JSON Viewer did not load.")), timeout));
+
+  return Promise.race([onJSONViewInitialized, onTimeout]);
 }
 
 /**
  * Expanding a node in the JSON tree
  */
 function clickJsonNode(selector) {
   info("Expanding node: '" + selector + "'");
 
@@ -160,19 +153,17 @@ function sendString(str, selector) {
     selector: selector,
     str: str
   };
 
   return executeInContent("Test:JsonView:SendString", data);
 }
 
 function waitForTime(delay) {
-  let deferred = defer();
-  setTimeout(deferred.resolve, delay);
-  return deferred.promise;
+  return new Promise(resolve => setTimeout(resolve, delay));
 }
 
 function waitForFilter() {
   return executeInContent("Test:JsonView:WaitForFilter");
 }
 
 function normalizeNewLines(value) {
   return value.replace("(\r\n|\n)", "\n");