Live data from Hacker News

Comparing Node.js Promises, Try/Catch, Angular Zone.js and Zone

strongloop.com

1–10 of 21 posts

Re: Comparing Node.js Promises, Try/Catch, Angular Zone.js and Zone

#2
Cute, although if you promisify all your async primitives promises are throw safe and the Bluebird library makes this really easy.

I hope they're able to get to as fast as Bluebird performance , which is almost as fast as callbacks and faster than the async module.

Re: Comparing Node.js Promises, Try/Catch, Angular Zone.js and Zone

#3
Generators are the best, though I'm starting to worry I'm using them too much.

edit: I'm using koa, so co is working in the background here. Should have said that earlier, my bad.

var save = function*(){ try { yield db.insertUser(); } catch (e) { throw e; } }

Re: Comparing Node.js Promises, Try/Catch, Angular Zone.js and Zone

#4

Generators are the best, though I'm starting to worry I'm using them too much. edit: I'm using koa, so co is working in the background here. Should have said that earlier, my bad. var save = function*(){ try { yield db.insertUser(); } catch (e) { throw e; } }

have you tried Co (https://github.com/visionmedia/co) and if so what's your experience?

Re: Comparing Node.js Promises, Try/Catch, Angular Zone.js and Zone

#5

Generators are the best, though I'm starting to worry I'm using them too much. edit: I'm using koa, so co is working in the background here. Should have said that earlier, my bad. var save = function*(){ try { yield db.insertUser(); } catch (e) { throw e; } }

That's not entirely true. If "db.insertUser()" is opening a database connection, who is going to close it on error?

The idea behind Zones for Node.js is to auto-attach new resources to the current zone, so that they can be cleaned when the zone exits.

AFAIK there is no solution for that at the moment.

Re: Comparing Node.js Promises, Try/Catch, Angular Zone.js and Zone

#7
The author does not seem like an experienced Node.js developer. All those

    if (err) {
      done(err);
    }
    else {
      // ...
    }
are usually written in this style:

   if (err) return done(err);
   // ...
I personally don't like promises and tend to avoid libraries who use them. I don't really see the problem they are trying to solve which is not already solvable in a more flexible way through libraries like async.

Re: Comparing Node.js Promises, Try/Catch, Angular Zone.js and Zone

#8
post #7

The author does not seem like an experienced Node.js developer. All those if (err) { done(err); } else { // ... } are usually written in this style: if (err) return done(err); // ... I personally don't like promises and tend to avoid libraries who use them. I don't really see the problem they are trying to solve which is not already solvable in a more flexible way through libraries like async.

I agree that libraries should not be handling errors in any unusual way, or better yet just re/throw them and let consumer take care of that. That's where the promises/zones might be useful.

Re: Comparing Node.js Promises, Try/Catch, Angular Zone.js and Zone

#9
> Up until recently that was pretty much all we had.

[Edit: see my comment below[0]. This is more a coincidence of wording than a StrongLoop marketing line]

When StrongLoop talked at my south bay node.js meetup group, BayNode[1], at Hacker Dojo last month, they demoed Zone.js. Before demoing though, they asked if anyone in the group had "solved this" to which I made it very clear that, "Yes, I did about 2 years ago with trycatch[2]." Their response could be summed up as, "Oh?"

Long story short, not only does my async try/catch library solve this, I can say with near certainty that it solves it in a better, more consistent, more tested, more battle proven, more performant manner. We use trycatch at LinkedIn and have not had to worry about async error handling since.

Additionally, adding it to any control-flow of your choice is trivial, as I have done with my stepup library[3]. Further, their main bullet-points are about enforcing the callback contract, something EVERY control-flow library should be doing, (I previously went into more detail here[4]) and the primary reason for the popularity of promises since they formalized this contract.

In fact, I teach these core contract rules in my monthly week-long node.js bootcamp I give here at LinkedIn[5], and the only thing they have to do with control-flow is that your control-flow library of choice should enforce them, as stepup and promises do. I do add a few rules:

* Function that takes 2 arguments: 1. first argument is an error, 2. second argument is the result, 3. Never pass both, 4. error should be instanceof Error

* Must never excecute on the same tick of the event loop

* Must be passed as last argument to function

* Return value is ignored

* Must not throw / must pass resulting errors

* Must never be called more than once

Long story short, domains are broken, try/catch is insufficient, use trycatch because I solved this problem over 2 years ago and have been perfecting it since.

[0] https://news.ycombinator.com/item?id=7598983

[1] http://www.meetup.com/BayNode/

[2] https://github.com/CrabDude/trycatch

[3] https://github.com/CrabDude/stepup

[4] https://news.ycombinator.com/item?id=7020054

[5] https://gist.github.com/CrabDude/10907185

Re: Comparing Node.js Promises, Try/Catch, Angular Zone.js and Zone

#10
post #7

The author does not seem like an experienced Node.js developer. All those if (err) { done(err); } else { // ... } are usually written in this style: if (err) return done(err); // ... I personally don't like promises and tend to avoid libraries who use them. I don't really see the problem they are trying to solve which is not already solvable in a more flexible way through libraries like async.

I agree that libraries should not be handling errors in any unusual way, or better yet just re/throw them and let consumer take care of that. That's where the promises/zones might be useful.

Right. I don't mind if people want to use promises in their own code as long as they don't force their use on library users.
Post reply on HN