-
Couldn't load subscription status.
- Fork 2k
Description
- update: Looks like tamejs has changed their syntax a little since July:
twaitis now calledawaitandmkeventis now calleddefer. Luckily, they don't appear to have changed any of the semantics, though I haven't looked that hard. Take that into account when checking out tamejs. - (originally from a comment in Add syntax for handling nested callbacks #1704)
A few months ago, I was checking out tamejs, liked the ideas, and started thinking about how it could be incorporated into coffeescript. For the uninitiated: tamejs basically just takes the JS you write and compiles it to use continuation-passing style. So the output's not so pretty. Anyway, I took a few of the examples from the website, pasted them into a gist, and rewrote them in what I called "imaginary-coffeescript-with-defer". I'll include one gist inline and just link to the other.
Imaginary CoffeeScript
{resolve} = require "dns"
do_one = (host, cb) ->
(err, ip) <- resolve host, "A", *
console.log if err then "ERROR! #{err}" else "#{host} -> #{ip}"
cb?()
do_all = (hosts) ->
defer
for host, i in hosts
do_one host, null
return
do_all process.argv[2..]Original tamejs Example
var dns = require("dns");
function do_one (ev, host) {
var err, ip;
twait { dns.resolve (host, "A", mkevent (err, ip));}
if (err) { console.log ("ERROR! " + err); }
else { console.log (host + " -> " + ip); }
ev();
}
function do_all (lst) {
twait {
for (var i = 0; i < lst.length; i++) {
do_one (mkevent (), lst[i]);
}
}
}
do_all (process.argv.slice (2));There's a pretty simple mapping from the added coffeescript constructs to the tamejs additions.
deferblock is just one bigtwait(arg0, arg1, ..., argN) <- expression:- compile to a
twaitunless inside adefer - save args for compilation of any bare
*(or whatever syntax we pick) insideexpression
- compile to a
- bare
*(or whatever syntax we pick) compiles tomkeventusing args from containing<-
Now I'm not sure how appropriate it would be to add to CS because of the possibly irreparably ugly compilation. But I think it's worth a discussion even considering the numerous, extremely lengthy tickets on defer-style constructs. Hell, I think people would sacrifice the readable output for a powerful feature like that. And they would only need to do so when using that feature.
I think it makes a really good use of both <- and defer. That syntax just really seems to fit their proposed functionality.
Pinging list of tamejs contributors: @maxtaco, @malgorithms, @m8apps, @frew