Created
September 17, 2021 01:21
-
-
Save itsjohncs/f95df23cc40adf1c2a3fe761e52733c9 to your computer and use it in GitHub Desktop.
One works and the other freezes the browser... Why?
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<script> | |
let _openCVPromise = null; | |
function getOpenCV() { | |
if (_openCVPromise === null) { | |
_openCVPromise = new Promise(function(resolve, reject) { | |
const script = document.createElement("script"); | |
script.setAttribute("src", "https://shmeppy-external-js.sfo3.cdn.digitaloceanspaces.com/opencv-4.5.3.js"); | |
script.addEventListener("load", function() { | |
resolve(window.cv); // !! Only difference | |
}); | |
document.head.appendChild(script); | |
}); | |
} | |
return _openCVPromise; | |
} | |
</script> | |
</head> | |
<body> | |
<button onClick="console.log(getOpenCV())">load</button> | |
<button onClick="console.log('OpenCV loaded', !!window.cv)">Beep</button> | |
</body> | |
</html> |
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<script> | |
let _openCVPromise = null; | |
function getOpenCV() { | |
if (_openCVPromise === null) { | |
_openCVPromise = new Promise(function(resolve, reject) { | |
const script = document.createElement("script"); | |
script.setAttribute("src", "https://shmeppy-external-js.sfo3.cdn.digitaloceanspaces.com/opencv-4.5.3.js"); | |
script.addEventListener("load", function() { | |
resolve(); // !! Only difference | |
}); | |
document.head.appendChild(script); | |
}); | |
} | |
return _openCVPromise; | |
} | |
</script> | |
</head> | |
<body> | |
<button onClick="console.log(getOpenCV())">load</button> | |
<button onClick="console.log('OpenCV loaded', !!window.cv)">Beep</button> | |
</body> | |
</html> |
Ahhhh that makes sense. Thank you for solving this mystery.
Though I have a new mystery. I forget why I made this gist. Where did you find a link to this?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
After many hours of bug hunting I found this out for my own sake, feeling quite triumphant!
The reason that
resolve(window.cv);
freezes the browser is thatcv
is infinitely thenable, so an infinite recursion is started.resolve
checks if the provided argument is thenable, and if so hooks up to it via itsthen
method, passingresolve
as argument I suppose. And if OpenCV has been initialized, then itsthen
method calls the provided function with OpenCV itself as a argument, which has athen
method, which is called and calls back with itself, which has athen
method, which is called and calls back with itself, and so on...My solution was to wrap it:
resolve({cv: window.cv})
.