From a94cf1976c6b78d8798eca843977ea9d74ca693e Mon Sep 17 00:00:00 2001 From: Mikal Stordal Date: Sun, 29 Sep 2024 08:58:38 +0200 Subject: [PATCH] Fix for async/await A small tweak to allow async/await syntax at the top-level of the module to work properly because the script.onload event is ran immediately after the script is loaded, but before the module exports are available. --- importModule.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/importModule.js b/importModule.js index 065a03d..f78c408 100644 --- a/importModule.js +++ b/importModule.js @@ -22,12 +22,12 @@ export function importModule(url) { reject(new Error(`Failed to import: ${url}`)); destructor(); }; - script.onload = () => { - resolve(window[vector]); + window[vector] = (value) => { + resolve(value); destructor(); }; const absURL = toAbsoluteURL(url); - const loader = `import * as m from "${absURL}"; window.${vector} = m;`; // export Module + const loader = `import * as m from "${absURL}"; window.${vector}(m);`; // export Module const blob = new Blob([loader], { type: "text/javascript" }); script.src = URL.createObjectURL(blob);