Skip to content

Instantly share code, notes, and snippets.

@carld
Created April 22, 2025 09:03
Show Gist options
  • Save carld/274c077f25afc355a66e8caba82e2dbb to your computer and use it in GitHub Desktop.
Save carld/274c077f25afc355a66e8caba82e2dbb to your computer and use it in GitHub Desktop.
promise/monad example in javascript
<html>
<head>
<style>
html * {
font-family: monospace;
}
</style>
</head>
<body>
<form onsubmit="return run();">
<textarea id="input"></textarea>
<input type="submit"></input>
</form>
<div id="output"></div>
</body>
<script>
const ret = x => Promise.resolve(x); //new Promise((s,f) => s(x));
const bind = (m, f) => m.then(v => f(v));
const pipe = (fn, ...fns) => fns.reduce(bind, fn);
const r = pipe( ret(1), x => x * 2, x => x + 1, x => x + 10);
console.log("Test:", r);
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment