node-learnin'

14 thoughts
last posted June 27, 2015, 9:24 p.m.
0
get stream as: markdown or atom
0

I haven't posted in over a year because I'm primarily a Node developer now. I still don't understand the advantages of [almost] everything being async, but I feel like it must be better or why the hell would anyone deal with callback hell. Also our stuff scales really well and I never have to think about threads. Whenever we find something slow, it's almost always in rendering Express views... not any dev-opsy thing.

I've casually read up on Python's asyncio, but until I use it I won't understand it. I think with asyncio, you have to explicitly decide what is async, instead of things being async by default. It seems like asyncio is really low-level, and you have to manage a lot of the things that Node handles for you. It doesn't seem very fun to use.

Additionally, the Node community has grown so fast. While I used to be able to find a Python library for just about anything, now I have trouble. But, I can usually find several Node libraries.

I really miss so much about Python, and I still don't like writing Javascript, but Node is usually a more practical choice for me now.

0

Node debugging hack

I was getting stack traces from errors that had been passed back to several callbacks, and I couldn't figure out where the calling code was that started off the chain. So I came up with this ugly beast.

function injector(callback) {
    var trace = new Error().stack;
    return function(err, res) {
        if (err) {
            err.injection += trace + '\n' + new Error().stack + '\n\n';
        }
        callback(err, res);
    };
}

Which can then be called like so with async:

var myLib = require('myLib');

async.each([
    function oneThing(cb) {
        myLib.doSomethingAsync(injector(cb));
    },
    function twoThing(cb) {
       myLib.aDifferentAsync(injector(cb));
    }
], function(err, res) {
    // handle results
});

In injector, the first call to new Error().stack will tell you whether it was oneThing or twoThing that ultimately generated the error. The second will tell you where in myLib the call came from.

Before this my stack traces only had information from deep in the dependency tree.

I haven't tried this yet on a callback with an error that's already been injected. I wonder if all that info would be needed or if the most recent call would be enough.


Just realized, since I'm not using this in production, I can do this instead:

function injector(callback) {
    var trace = new Error().stack;
    return function(err, res) {
        if (err) {
            console.trace(trace);
        }
        callback(err, res);
    };
}
0

I wish I could use Nunjucks instead of EJS. I think it has enough embedded logic to do the stuff we do.

0

EJS is hideous. I hate looking at it's stupid face.

0

So many useless stacktraces.

0

I'm still not clear on what the advantages are of working with callbacks and asychronicity and event loops and such. I get that it can be useful for the app to be doing more than one thing at a time... but how much of a difference does that really make?

0

I'm reading this node book and I'm really impressed by the simplicity and elegance in writing node and express apps. Of course, the book has trivial examples, but I can imagine being able to keep it simple even with a "real" app.

Aside from having to write Javascript instead of Python, I think I will enjoy writing node apps.

I should really play with Flask though... it might just be that this is the first I've really dived into a micro-framework.

0

Yeah node is definitely not the new PHP. You have to know what the fuck you're doing to use node.

Ok, I suppose you don't have to, but there's a lot more to it that you should than the basics of "make web app go", which is all PHP is.

0

I don't think I'll ever grok the event loop.

0

I've finished my first week at my new job where I'm a node developer. I'm very impressed with the code my new colleagues have written. I've heard people say that node is the new PHP, but from what I've seen so far I don't think that's true.

We're also using MongoDB in smart ways, which surprised me considering all the jokes about how bad it is.

I do miss Python, though, I did write a fab script to get my node dev env up and running.

Mostly I'm worried I'll lose touch with the Python community. I'm not going to PyCon this year like I hoped (I have never been before either). I was on the fence for a while, and then I decided to go, but before I got around to it, the tickets were sold out. Probably for the best because I don't think I could have taken vacation at that time anyway.

Having a regular job now I only get 2 weeks vacation a year, I need that to see family, so I probably won't be going to any conferences unless work sends me, and they don't have a reason to send me to a Python conference.

That was a bit rambly, but hey, it's called "thoughstreams" after all.

0

"As a guideline, if the relationship between exports and module.exports seems like magic to you, ignore exports and only use module.exports." -- yes, ok, thank you for this because I still don't get it.

0

Ok, I read the Express API docs and it seems pretty cool. It's straight-forward and fairly elegant too.

0

EJS templating is giving me horrible flashbacks to PHP development.

0

I had to test this to make sure it was true before saying... go home JavaScript... you're drunk.

10:55:57 🌵 node
> var test = [1, 2, 3, 11, 15, 21, 10]
undefined
> test.sort()
[ 1,
  10,
  11,
  15,
  2,
  21,
  3 ]