Created
April 22, 2025 09:03
-
-
Save carld/274c077f25afc355a66e8caba82e2dbb to your computer and use it in GitHub Desktop.
promise/monad example in javascript
This file contains hidden or 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
<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