Last active
August 23, 2023 16:34
-
-
Save noamr/756d349e2a9457c643837eab1143dc0e to your computer and use it in GitHub Desktop.
A polyfill for script element loading (for illustration purposes)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function parseHTML(getNextElement) { | |
let doneParsing = null; | |
const whenDoneParsing = new Promise(resolve => { doneParsing = resolve; }); | |
while (const element = getNextElement()) { | |
if (element.tagName !== "SCRIPT") { | |
// parse everything else... | |
continue; | |
} | |
const whenSourceLoaded = fetch(element.src).then(response => response.text()); | |
if (element.async) { | |
whenSourceLoaded.then(eval); | |
} else if (element.type === "module" || element.defer) { | |
Promise.all([whenSourceLoaded, whenDoneParsing]).then(([source]) => eval(source)); | |
} else { | |
eval(await whenSourceLoaded); | |
} | |
} | |
// This will execute all scripts that are already loaded in the same task | |
doneParsing(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
OK
OK actually it was a mistake, was thrown off by "force deferred" script which is a non-standard chromium feature.