4 ms·
I hope that everyone who writes JavaScript will do themselves a favor and try out ToffeeScript. This offshoot of CoffeeScript simplifies things. For example:
by ilaksh 13y ago
I hope that everyone who writes JavaScript will do themselves a favor and try out ToffeeScript. This offshoot of CoffeeScript simplifies things. For example:
if fs.exists! thefile
e, datastr = fs.readFile! thefile, 'utf8'
else
datastr = 'not found'
success = db.update! 'somekey', datastr
res.end success
So in that example even though there are three callbacks it looks like a normal synchronous flow.
- lucaspiller 13y agoI haven't heard of ToffeeScript before, but this is pretty much how Erlang works. Whenever you make a blocking call, all of the async stuff happens under the hood, so that you just deal with sequential code. Erlang's concurrency model deals with lots of lightweight processes. Rather than having callbacks it uses a message system. Whenever a process waits for a message it yields so other processes can be executed. In your example there would be a process separate from the main[0] process that handles the database connection. When you request the update to the database, you are just sending a message to the database connection process. This will receive the message and handle the update when it can[1], and once complete, send you a message back with the result of the update. During the waiting period the VM will continue executing other processes. No callbacks needed :) [0] Erlang concurrency is based upon the actor model. There isn't really a main process, but for the sake of explanation... [1] The database connection process will actually block while making the update, so if you have 10,000 processes calling this, you will have a bottleneck. As such you should have a pool of database connection processes, rather than just one.
- ilaksh 13y agoYeah Erlang looks awesome and I should definitely try to learn it at some point. To me Node.js and ToffeeScript seem a lot more straightforward and have the advantage of the whole package system which I believe is more advanced, as well as the npm ecosystem with 54,000 packages. But anyway it would be good to actually learn Erlang and use it in some projects so that I can really evaluate it. I'm not sure but I think it might be a lot more resource efficient for one thing.
- seoguru 13y agoHave you tried IcedCoffeescript? http://maxtaco.github.io/coffee-script/ http://maxtaco.github.io/coffee-script/ if so, how do you think it compares to ToffeeScript?
- ilaksh 13y agoYes I have used IcedCoffeeScript. The main idea with IcedCoffeeScript is very similar to the main advantage of ToffeeScript. Namely synchronous looking async using await and defer. ToffeeScript is definitely superior because it allows you to do that using a much cleaner syntax, just using the ! operator. ToffeeScript also has some other features.
- seoguru 13y agoYes, the syntax does look cleaner. I wish the documentation was a little better, but hey, beggars can't be choosers :-)
- babby 13y agoAlso IcedCoffeeScript http://maxtaco.github.io/coffee-script/ http://maxtaco.github.io/coffee-script/ It's just CoffeeScript with await and defer keywords, essentially. I personally would never touch toffeescript/coco and friends as they differ too much for CoffeeScript, which already segregates the JS userbase enough. I figure IcedCoffeeScript is the lesser evil for compile-time control flow as it's not adding a bunch of other changes to the language that users have to understand to contribute to a project written in it. Another issue with these CS spinoffs is; what if the main contributor says "Fuck it" and bails? CS itself is big enough to have a lot of talented contributors. What happens when CS moves over to CS 2.0 ie. coffeescript redux? Do these spinoffs need to recoded? That being said I probably won't use Iced either, as much as I want to... Maybe I'm all wrong about this? Maybe this userbase segregation crap is not as big a deal as I may think.
- ilaksh 13y agoI only use that one feature from ToffeeScript as shown in my example. Obviously much cleaner