Created
July 13, 2023 22:03
-
-
Save mlhaufe/04fc8f9cdc075123bb821e3118a5121a to your computer and use it in GitHub Desktop.
callcc
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
class ContError extends Error { returnValue = undefined; } | |
function callcc(fnCont) { | |
const contErr = new ContError("Unable to continue current continuation."); | |
const fnEscape = (returnValue) => { | |
contErr.returnValue = returnValue; | |
throw contErr; | |
} | |
try { | |
return fnCont(fnEscape); | |
} catch (e) { | |
if (e === contErr) | |
return contErr.returnValue; | |
else | |
throw e; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment