5 ms·
This is a pretty common pattern for any work you have to do asynchronously, pretty much all libraries should be implementing this for you so the first 3 lines s
by daleharvey 15y ago
This is a pretty common pattern for any work you have to do asynchronously, pretty much all libraries should be implementing this for you so the first 3 lines should be all you code
getSomething("id", function(thething) {
// one true code path
});
function getSomething(id, callback) {
var myThing = synchronousCache.get("id:3244");
if(myThing) {
callback(null, myThing);
} else {
async(id, callback);
}
}
a minor quibble with language style isnt exactly what I would call "A Giant Step Backwards"
- howsta 15y agoAmen to that. The power of Node is this language style already makes tons of sense to experienced front-end developers who deal with async flow for everything they do.
- Fenn 15y agoI heartily agree - I may have overstated my case slightly with the "Giant Step Backwards", it just definitely seemed that way on initial experience. It also made for a better title than "Confusion, Then Indifference, Slowly Turning Into Understanding & Affinity"
- randylahey 15y agoIt's kind of a dick move to make a inflammatory, flamebait blog post and then later admit you didn't actually know what you were talking about. Maybe you should have actually understood the technology you were working with before making knee-jerk snap judgements.
- gcr 15y agoHow was your first experience with node.js different than Fenn's?
- randylahey 15y agoSure, everyone has to start somewhere, and to be perfectly honest, I ran into many of the same issues. The difference is in what happened afterward. What I did then was to try to understand why Node.js did things differently and how I could accomplish my goals in an idiomatic way. I didn't try to shoe-horn in my existing mental framework for how things should work, and then throw my hands in the air when they didn't. However, what I didn't do was immediately run to Alert the Internets about what "A Giant Step Backward" Node.js is.
- rodion_89 15y agoHonestly, if you have ever done anything substantial in JavaScript then asynchronous functions with a callback pattern should not be such an issue. Perhaps if your background is more on the lines of traditional server-side web development (ie. PHP, Django, Rails) then this is some new territory.
- benatkin 15y agoIt's not an inflammatory blog post. The headline might be a little annoying to some, but not to most of us. There are negative headlines about every programming language platform. Also it's a reminder for Node.js developers to make good use of async patterns, lest their code look silly. Besides async, which Fenn mentions in the comments, there's EventEmitters and Backbone.js for doing different styles of async programming. And there are a few other libraries that are a lot like https://github.com/caolan/async https://github.com/caolan/async .
- gizzlon 15y agovar myThing = synchronousCache.get("id:3244"); I was under the impression that you could not do _anything_ synchronous? What if the call blocks for 100ms? or 1000ms? Won't that delay all other clients and all other requests?