diff options
author | Juanjo Diaz <juanjo.diazmo@gmail.com> | 2019-02-17 14:12:28 +0200 |
---|---|---|
committer | Juanjo Diaz <juanjo.diazmo@gmail.com> | 2019-02-27 10:18:59 +0200 |
commit | 1c9b904d1aa585e4f096fe922ca508bdb4c5106a (patch) | |
tree | b2d1a25ea637ed3266e256ee32867a12fc7e175a /app/webutil.js | |
parent | 41ddb35458f2b64d1aaa2d262c7130e069ea2d99 (diff) | |
download | novnc-1c9b904d1aa585e4f096fe922ca508bdb4c5106a.tar.gz |
Remove callbacks from UI in favour of promises
Diffstat (limited to 'app/webutil.js')
-rw-r--r-- | app/webutil.js | 62 |
1 files changed, 28 insertions, 34 deletions
diff --git a/app/webutil.js b/app/webutil.js index 922ddc1..98e1d9e 100644 --- a/app/webutil.js +++ b/app/webutil.js @@ -114,22 +114,14 @@ export function eraseCookie(name) { let settings = {}; -export function initSettings(callback /*, ...callbackArgs */) { - "use strict"; - const callbackArgs = Array.prototype.slice.call(arguments, 1); - if (window.chrome && window.chrome.storage) { - window.chrome.storage.sync.get((cfg) => { - settings = cfg; - if (callback) { - callback.apply(this, callbackArgs); - } - }); - } else { +export function initSettings() { + if (!window.chrome || !window.chrome.storage) { settings = {}; - if (callback) { - callback.apply(this, callbackArgs); - } + return Promise.resolve(); } + + return new Promise(resolve => window.chrome.storage.sync.get(resolve)) + .then((cfg) => { settings = cfg; }); } // Update the settings cache, but do not write to permanent storage @@ -218,28 +210,30 @@ export function injectParamIfMissing(path, param, value) { // IE11 support or polyfill promises and fetch in IE11. // resolve will receive an object on success, while reject // will receive either an event or an error on failure. -export function fetchJSON(path, resolve, reject) { - // NB: IE11 doesn't support JSON as a responseType - const req = new XMLHttpRequest(); - req.open('GET', path); - - req.onload = () => { - if (req.status === 200) { - let resObj; - try { - resObj = JSON.parse(req.responseText); - } catch (err) { - reject(err); +export function fetchJSON(path) { + return new Promise((resolve, reject) => { + // NB: IE11 doesn't support JSON as a responseType + const req = new XMLHttpRequest(); + req.open('GET', path); + + req.onload = () => { + if (req.status === 200) { + let resObj; + try { + resObj = JSON.parse(req.responseText); + } catch (err) { + reject(err); + } + resolve(resObj); + } else { + reject(new Error("XHR got non-200 status while trying to load '" + path + "': " + req.status)); } - resolve(resObj); - } else { - reject(new Error("XHR got non-200 status while trying to load '" + path + "': " + req.status)); - } - }; + }; - req.onerror = evt => reject(new Error("XHR encountered an error while trying to load '" + path + "': " + evt.message)); + req.onerror = evt => reject(new Error("XHR encountered an error while trying to load '" + path + "': " + evt.message)); - req.ontimeout = evt => reject(new Error("XHR timed out while trying to load '" + path + "'")); + req.ontimeout = evt => reject(new Error("XHR timed out while trying to load '" + path + "'")); - req.send(); + req.send(); + }); } |