Last active
February 26, 2025 13:57
-
Star
(208)
You must be signed in to star a gist -
Fork
(20)
You must be signed in to fork a gist
-
-
Save sebmarkbage/2c7acb6210266045050632ea611aebee to your computer and use it in GitHub Desktop.
SynchronousAsync.js
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
let cache = new Map(); | |
let pending = new Map(); | |
function fetchTextSync(url) { | |
if (cache.has(url)) { | |
return cache.get(url); | |
} | |
if (pending.has(url)) { | |
throw pending.get(url); | |
} | |
let promise = fetch(url).then( | |
response => response.text() | |
).then( | |
text => { | |
pending.delete(url); | |
cache.set(url, text); | |
} | |
); | |
pending.set(url, promise); | |
throw promise; | |
} | |
async function runPureTask(task) { | |
for (;;) { | |
try { | |
return task(); | |
} catch (x) { | |
if (x instanceof Promise) { | |
await x; | |
} else { | |
throw x; | |
} | |
} | |
} | |
} |
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
function getUserName(id) { | |
var user = JSON.parse(fetchTextSync('/users/' + id)); | |
return user.name; | |
} | |
function getGreeting(name) { | |
if (name === 'Seb') { | |
return 'Hey'; | |
} | |
return fetchTextSync('/greeting'); | |
} | |
function getMessage() { | |
let name = getUserName(123); | |
return getGreeting(name) + ', ' + name + '!'; | |
} | |
runPureTask(getMessage).then(message => console.log(message)); |
This is brilliant dude, I am intrigued about algebraic effects now 🤩🤩🤩🤩
Huh, pretty cool.
The major downside is not the throw, it is that you may have to reuse some work if it does.
By this, do you mean that the "infinite" for
loop will keep executing same code from start after each await
, right?
@sebmarkbage
how is this better than writing fetchTextSync
as generator function and using npm/co
(Generator based control flow) instead of runPureTask
. I mean yes, you doesn't bound to using genearor in your example, but still depends on async runPureTask
.
I know it's different concepts and so on, but maybe you could explain why is your approach is generally better and what benefits it provides?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@jaredpalmer @sebmarkbage
I added promise rejection scenario (see "try this" comment) and composition of fetchTextSync:
https://gist.github.com/idibidiart/69fd5f9df339b5ef1783e6a8fae9fa51
I like how I can compose idiomatically with this, e.g.: let result = fetchSync('/test/1a') + ' ' + fetchSync('/test/1b')