Skip to content

Instantly share code, notes, and snippets.

@mlhaufe
Created July 13, 2023 22:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mlhaufe/04fc8f9cdc075123bb821e3118a5121a to your computer and use it in GitHub Desktop.
Save mlhaufe/04fc8f9cdc075123bb821e3118a5121a to your computer and use it in GitHub Desktop.
callcc
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