From 4d647f3a329e3e7982e6625e81628cc356c6e404 Mon Sep 17 00:00:00 2001 From: Tim Taubert Date: Thu, 25 Sep 2014 10:06:07 +0200 Subject: [PATCH] Bug 1072814 - Convert trivial Promise.defer() usages to new Promise() r=yoric --- .../components/sessionstore/SessionStore.jsm | 18 ++-- browser/components/sessionstore/test/head.js | 91 +++++++++---------- 2 files changed, 52 insertions(+), 57 deletions(-) diff --git a/browser/components/sessionstore/SessionStore.jsm b/browser/components/sessionstore/SessionStore.jsm index 8b8d41ea37f..a79fc6143f9 100644 --- a/browser/components/sessionstore/SessionStore.jsm +++ b/browser/components/sessionstore/SessionStore.jsm @@ -911,20 +911,20 @@ let SessionStoreInternal = { // re-used by all subsequent windows. The promise will be used to tell // when we're ready for initialization. if (!this._promiseReadyForInitialization) { - let deferred = Promise.defer(); - // Wait for the given window's delayed startup to be finished. - Services.obs.addObserver(function obs(subject, topic) { - if (aWindow == subject) { - Services.obs.removeObserver(obs, topic); - deferred.resolve(); - } - }, "browser-delayed-startup-finished", false); + let promise = new Promise(resolve => { + Services.obs.addObserver(function obs(subject, topic) { + if (aWindow == subject) { + Services.obs.removeObserver(obs, topic); + resolve(); + } + }, "browser-delayed-startup-finished", false); + }); // We are ready for initialization as soon as the session file has been // read from disk and the initial window's delayed startup has finished. this._promiseReadyForInitialization = - Promise.all([deferred.promise, gSessionStartup.onceInitialized]); + Promise.all([promise, gSessionStartup.onceInitialized]); } // We can't call this.onLoad since initialization diff --git a/browser/components/sessionstore/test/head.js b/browser/components/sessionstore/test/head.js index b718ba7d56f..f3eb50d5975 100644 --- a/browser/components/sessionstore/test/head.js +++ b/browser/components/sessionstore/test/head.js @@ -197,21 +197,21 @@ function waitForTabState(aTab, aState, aCallback) { * Wait for a content -> chrome message. */ function promiseContentMessage(browser, name) { - let deferred = Promise.defer(); let mm = browser.messageManager; - function removeListener() { - mm.removeMessageListener(name, listener); - } + return new Promise(resolve => { + function removeListener() { + mm.removeMessageListener(name, listener); + } - function listener(msg) { - removeListener(); - deferred.resolve(msg.data); - } + function listener(msg) { + removeListener(); + resolve(msg.data); + } - mm.addMessageListener(name, listener); - registerCleanupFunction(removeListener); - return deferred.promise; + mm.addMessageListener(name, listener); + registerCleanupFunction(removeListener); + }); } function waitForTopic(aTopic, aTimeout, aCallback) { @@ -259,14 +259,15 @@ function waitForSaveState(aCallback) { return waitForTopic("sessionstore-state-write-complete", timeout, aCallback); } function promiseSaveState() { - let deferred = Promise.defer(); - waitForSaveState(isSuccessful => { - if (isSuccessful) { - deferred.resolve(); - } else { - deferred.reject(new Error("timeout")); - }}); - return deferred.promise; + return new Promise(resolve => { + waitForSaveState(isSuccessful => { + if (!isSuccessful) { + throw new Error("timeout"); + } + + resolve(); + }); + }); } function forceSaveState() { return SessionSaver.run(); @@ -305,9 +306,9 @@ function whenBrowserLoaded(aBrowser, aCallback = next, ignoreSubFrames = true, e }); } function promiseBrowserLoaded(aBrowser, ignoreSubFrames = true) { - let deferred = Promise.defer(); - whenBrowserLoaded(aBrowser, deferred.resolve, ignoreSubFrames); - return deferred.promise; + return new Promise(resolve => { + whenBrowserLoaded(aBrowser, resolve, ignoreSubFrames); + }); } function whenBrowserUnloaded(aBrowser, aContainer, aCallback = next) { aBrowser.addEventListener("unload", function onUnload() { @@ -316,9 +317,9 @@ function whenBrowserUnloaded(aBrowser, aContainer, aCallback = next) { }, true); } function promiseBrowserUnloaded(aBrowser, aContainer) { - let deferred = Promise.defer(); - whenBrowserUnloaded(aBrowser, aContainer, deferred.resolve); - return deferred.promise; + return new Promise(resolve => { + whenBrowserUnloaded(aBrowser, aContainer, resolve); + }); } function whenWindowLoaded(aWindow, aCallback = next) { @@ -330,9 +331,7 @@ function whenWindowLoaded(aWindow, aCallback = next) { }, false); } function promiseWindowLoaded(aWindow) { - let deferred = Promise.defer(); - whenWindowLoaded(aWindow, deferred.resolve); - return deferred.promise; + return new Promise(resolve => whenWindowLoaded(aWindow, resolve)); } function whenTabRestored(aTab, aCallback = next) { @@ -470,9 +469,7 @@ function whenNewWindowLoaded(aOptions, aCallback) { return win; } function promiseNewWindowLoaded(aOptions) { - let deferred = Promise.defer(); - whenNewWindowLoaded(aOptions, deferred.resolve); - return deferred.promise; + return new Promise(resolve => whenNewWindowLoaded(aOptions, resolve)); } /** @@ -480,17 +477,17 @@ function promiseNewWindowLoaded(aOptions) { * a window and wait until we received the "domwindowclosed" notification for it. */ function promiseWindowClosed(win) { - let deferred = Promise.defer(); - - Services.obs.addObserver(function obs(subject, topic) { - if (subject == win) { - Services.obs.removeObserver(obs, topic); - deferred.resolve(); - } - }, "domwindowclosed", false); + let promise = new Promise(resolve => { + Services.obs.addObserver(function obs(subject, topic) { + if (subject == win) { + Services.obs.removeObserver(obs, topic); + resolve(); + } + }, "domwindowclosed", false); + }); win.close(); - return deferred.promise; + return promise; } function runInContent(browser, func, arg, callback = null) { @@ -517,7 +514,7 @@ function whenDelayedStartupFinished(aWindow, aCallback) { }, "browser-delayed-startup-finished", false); } function promiseDelayedStartupFinished(aWindow) { - return new Promise((resolve) => whenDelayedStartupFinished(aWindow, resolve)); + return new Promise(resolve => whenDelayedStartupFinished(aWindow, resolve)); } /** @@ -571,14 +568,12 @@ function next() { } function promiseTabRestored(tab) { - let deferred = Promise.defer(); - - tab.addEventListener("SSTabRestored", function onRestored() { - tab.removeEventListener("SSTabRestored", onRestored); - deferred.resolve(); + return new Promise(resolve => { + tab.addEventListener("SSTabRestored", function onRestored() { + tab.removeEventListener("SSTabRestored", onRestored); + resolve(); + }); }); - - return deferred.promise; } function sendMessage(browser, name, data = {}) {