From 6b18c90228dc0dece666d2eee5ba8c356fe78372 Mon Sep 17 00:00:00 2001 From: Victor Westerlund Date: Fri, 21 Jan 2022 13:07:16 +0100 Subject: [PATCH] Remove promise anipattern --- monkey/Monkey.js | 22 ++++++++++------------ monkey/MonkeyMaster.mjs | 41 +++++++++++++++++++---------------------- 2 files changed, 29 insertions(+), 34 deletions(-) diff --git a/monkey/Monkey.js b/monkey/Monkey.js index d52dcbe..2232e05 100644 --- a/monkey/Monkey.js +++ b/monkey/Monkey.js @@ -75,19 +75,17 @@ class Monkey { // Install a Monkeydo manifest async loadManifest(manifest) { - return await new Promise((resolve,reject) => { - if(typeof manifest !== "object") { - try { - manifest = JSON.parse(manifest); - } - catch { - reject("Failed to load manifest"); - } + if(typeof manifest !== "object") { + try { + manifest = JSON.parse(manifest); } - this.tasks.manifest = manifest.tasks; - this.flags[0] = 1; // Manifest loaded: true - resolve(); - }); + catch { + Promise.reject("Failed to load manifest"); + } + } + this.tasks.manifest = manifest.tasks; + this.flags[0] = 1; // Manifest loaded: true + return true; } } diff --git a/monkey/MonkeyMaster.mjs b/monkey/MonkeyMaster.mjs index fd7c35d..3c46438 100644 --- a/monkey/MonkeyMaster.mjs +++ b/monkey/MonkeyMaster.mjs @@ -48,14 +48,12 @@ export default class MonkeyMaster { this.comlink = await new Monkey(); // Wait for comlink to spin up - return await new Promise((resolve,reject) => { - if(!this.comlink) reject("Failed to establish Comlink with worker"); + if(!this.comlink) Promise.reject("Failed to establish Comlink with worker"); - this.ready = true; - // Send queued flags when worker is ready - this.queue.sendAllFlags(); - resolve(); - }); + this.ready = true; + // Send queued flags when worker is ready + this.queue.sendAllFlags(); + return true; } // Return a flag array index by name @@ -104,21 +102,20 @@ export default class MonkeyMaster { // Load a Monkeydo manifest by URL or JSON string async loadManifest(manifest) { if(!this.ready) await this.init(); - return await new Promise((resolve,reject) => { - let load = null; - // Attempt load string as URL and fetch manifest - try { - const url = new URL(manifest); - // If the URL parsed but fetch failed, this promise will reject - load = this.comlink.fetchManifest(url.toString()); - } - // Or attempt to load string as JSON if it's not a URL - catch { - load = this.comlink.loadManifest(manifest); - } - load.then(() => resolve()) - .catch(() => reject("Failed to load manifest")); - }); + let load = null; + // Attempt load string as URL and fetch manifest + try { + const url = new URL(manifest); + // If the URL parsed but fetch failed, this promise will reject + load = this.comlink.fetchManifest(url.toString()); + } + // Or attempt to load string as JSON if it's not a URL + catch { + load = this.comlink.loadManifest(manifest); + } + + load.then(() => Promise.resolve()) + .catch(() => Promise.reject("Failed to load manifest")); } async stop() {