3 ms·
For callbacks I have found ToffeeScript to be an even bigger improvement than CoffeeScript. You can write code like this: e, personJson = fs.readFile! "pe
by ilaksh 13y ago
For callbacks I have found ToffeeScript to be an even bigger improvement than CoffeeScript. You can write code like this:
e, personJson = fs.readFile! "person.json"
person = JSON.parse personJson
matchesLastname = myDB.query! "lastname", person.name
id = msgQueue.push! { JSON.stringify(matchesLastname) }
log.info "Message sent to queue: #{id}"
instead of this:
fs.readFile("person.json", function(e, personJson) {
var person = JSON.parse(personJson);
myDB.query("lastname", person.name, function(matched) {
msgQueue.push(JSON.stringify(matchesLastname), function(id) {
log.info("Message sent to queue: " + id);
});
});
});
Why do you believe that it is better to design around serious issues rather than creating a new paradigm?
I have found that the best solutions always involve a new/different fundamental model that incorporates solutions to recurring problems.
- rubyn00bie 13y agoI guess I just don't think it's a serious issue. I've found that often when I have an "issue" it's more so my design lacking not the tool (javascript) itself.
- ilaksh 13y agoWhat would the code look like for my example using futures in JavaScript?
- TheZenPsycho 13y agofs.readFile("person.json") .then(function(e, personJson) { var person = JSON.parse(personJson); return myDB.query("lastname", person.name); }) .then(function(matched) { return msgQueue.push(JSON.stringify(matchesLastname)); }) .then(function(id) { log.info("Message sent to queue: " + id); });
- ilaksh 13y agoThat's part of it. What about creating the promises?
- TheZenPsycho 13y agothat's in there, with the assumption that you're using the promises based node.js api, but if you aren't, and you're using Q, that part looks like this var readFile = Q.denodeify(FS.readFile); which doesn't create the promise. it just wraps the readFile method so that it returns a promise when you call it.