Bug 1366531 - convert uses of defer to 'new Promise' in client/jsonview
MozReview-Commit-ID: InGMTAQ0X2R
--- 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");