by thom · 3y ago · 0 comments
8 years ago I wrote a npm package that turned code that looks like this:
tcp.send("syn", function(syn) {
console.log("received", syn);
tcp.send("syn-ack", function(synack) {
console.log("received", synack);
tcp.send("ack", function(ack) {
console.log("received", ack);
});
});
});
into this: seq([tcp, console], function(tcp, console) {
var syn = {}; var synack = {}; var ack = {};
tcp.send("syn", this(syn));
console.log("received %s", syn);
tcp.send("syn-ack", this(synack));
console.log("received %s", synack);
tcp.send("ack", this(ack));
console.log("received %s", ack);
});
it was never ready for production use, but it showed that synchronous code illusion can be created from callbacks.How would you prefer to write asynchronous code?
How does Loom handle resource starvation problem? If a thread puts a while (true) {} in there somewhere, can that thread be preempted away from that kernel thread?
If you put while (true) { } in an asynchronous function, wouldn't you also have a resource starvation problem? For what it's worth though, Loom threads are planned to be fully preemptible.
The worst part of Rust, too, is that async/await is tacked on— and it’s just a bad primitive to start with.
I have many more thoughts on this that I might like to blog about, but it can be improved by having a async runtime of some kind (we don’t call malloc a runtime, but it is— and you need something similar for ergonomic async) — to wit, the de facto pseudo-answer for this in Rust is tokio (which should just be promoted to the stdlib, at this point; the vast majority of async code depends on it’s particularities).
It’s also causes quote-unquote “function coloring problems” left, right, and center — which is also avoidable. Just for kicks, consider a language with “call-async”/“yield”. Tie that in with a runtime and this whole topic becomes much, much, cleaner. Async at the callsite! (This is the blog I’ve been meaning to write)