Live data from Hacker News

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

strongloop.com

11–20 of 21 posts

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

#11
post #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 w…

This seems really cool! I'm surprised I haven't heard about this until now.

However, this solution seems to be Node specific. The example where they lament about how this "was pretty much all we had" was about a browser error. Angular's Zone.js seems to be the closest project to solving the problem in that context.

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

#12
post #11
post #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 w…

This seems really cool! I'm surprised I haven't heard about this until now. However, this solution seems to be Node specific. The example where they lament about how this "was pretty much all we had" was about a browser error. Angular's Zone.js seems to be the closest project to solving the problem in that context.

Ah, sorry then for my strong rebuke. I am making the incorrect assumption then that this was a StrongLoop marketing line, not a coincidental similarity.

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

#13
post #11
post #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 w…

This seems really cool! I'm surprised I haven't heard about this until now. However, this solution seems to be Node specific. The example where they lament about how this "was pretty much all we had" was about a browser error. Angular's Zone.js seems to be the closest project to solving the problem in that context.

It's not StrongLoop marketing. I wrote the article and also haven't heard about your modules. I try to stay up to date with NPM (running http://npmawesome.com/) and unfortunately completely missed your work :(

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

#14

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; } }

A major limitation of using try/catch is that V8 will DEOPT your function call, which can be a 5-10x performance hit (to the current closure). The best way to avoid this is with a silly hack:

    function trycatchit(fn, that, args) {
      try {
        return fn.apply(that, args)
      } catch(e) {
       return e
      }
    }

    var value = trycatchit(fn, null, 'someValue')
    if (value instanceof Error) {
      ...
    }
Really though, you should just let co or whatever generator library you're using catch the error and rethrow it (or pass it to a callback) or be mindful to minimize your error handling.

[1] https://github.com/CrabDude/trycatch/blob/master/lib/trycatc...

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

#15
post #12
post #11

Earlier quoted context omitted.

This seems really cool! I'm surprised I haven't heard about this until now. However, this solution seems to be Node specific. The example where they lament about how this "was pretty much all we had" was about a browser error. Angular's Zone.js seems to be the closest project to solving the problem in that context.

Ah, sorry then for my strong rebuke. I am making the incorrect assumption then that this was a StrongLoop marketing line, not a coincidental similarity.

You should write a blog post about this. There are a lot of parallel efforts going on to bring sanity to async errors in JS, and it would be great to see what the outcome of cross-pollinating these ideas would be.

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

#16
One thing missing from this picture is Streams - the multivalued analog to Promises (Futures in Dart). The button click example would be quite easy to handle, even without Zones, if the button had an onClick event stream, rather than taking a callback.

This code, which doesn't work as intended:

  function thirdPartyFunction() {
     function fakeXHR() {
       throw new Error("Invalid dependencies");
     }
 
    setTimeout(fakeXHR, 100);
   }
 
  function main() {
     button.on("click", function onClick() {
       thirdPartyFunction();
     });
   }
 
  main();
would instead look like this with a Streams-based API (I'm assuming a Dart-like API, since I don't know the proposed Stream API for JS):

  function thirdPartyFunction() {
     function fakeXHR() {
       throw new Error("Invalid dependencies");
     }
 
    setTimeout(fakeXHR, 100);
   }
 
  function main() {
     button.on("click")
       .listen(function onClick() {
         thirdPartyFunction();
       })
       .onError(function onError() {
         console.log("now it works with errors");
       })
   }
 
  main();
Since onClick is executed by the Stream, even a synchronous exception in thirdPartyFunction will be caught and given to the onError callback. JavaScript could really use a better DOM API with Promises and Streams in place of most callbacks. I think most of the Streams work is here: https://github.com/whatwg/streams

So Zones aren't really needed here. They are very useful though. AngularDart uses them to intercept any external trigger of the event loop so that it can run it's change detection code (this is why they ported Zones to JavaScript). Dart's unittest library runs each test in a Zone to wait for outstanding microtasks and catch async errors that might happen after a test appears to have completed and associate it with the correct test. And of course using Zones to string together async stack traces or debugging is invaluable.

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

#17
post #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.

I'm thinking whatever db plugin you're using, but also this pseudo code actually has a lot of implicit things going in the background (koa/co, thunks, etc).

It makes for some readable app code though.

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

#18
post #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 w…

The article only covered error handling, but Zones are a lot more powerful than that. They are nestable (forkable), they can store arbitrary values, intercept microtasks, timers, wrap closures to be associated with the Zone, and a few more things.

Then the key part in Dart is that all of the APIs that invoke callbacks based on external events run the callbacks in the Zone they were registered in. This is what allows Zones to be used to easily run code at the beginning or end of every event loop turn.

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

#19
post #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 w…

The article only covered error handling, but Zones are a lot more powerful than that. They are nestable (forkable), they can store arbitrary values, intercept microtasks, timers, wrap closures to be associated with the Zone, and a few more things. Then the key part in Dart is that all of the APIs that invoke callbacks based on external events run the callbacks in the Zone they were registered in. This is what allows…

> They are nestable

trycatch is nestable as well.

> they can store arbitrary values, timers

Nice. I am currently decoupling trycatch's hooking layer to allow for this arbitrarily[1]. The continuation-local-storage library[2] allows for this functionality as well.

> intercept microtasks

Care to elaborate?

> wrap closures to be associated with the Zone

Yup, similar to domains, though I do wonder when this is necessary? One use case that came up with trycatch was finally support, or the need to exit the current domain/trycatch context.[3]

Lastly, there's one consistent failure I see in all these domain-like, async listener-like, long-stack-trace, event-source modules and that's long-lived resources or how they incorrectly handle EventEmitter handler's context[4], with the core issue being the boundary at which the hook is applied (From Trevor Norris' comment):

    After thinking this over, it occurred to me that trycatch is a top down approach. Whereas AsyncListener is bottom up.
Long story short, things like keep-alive sockets will retain a domain/context/zone(?) and their handlers will be called with the incorrect context.

Do you fix this in your zone.js implementation?

[1] https://github.com/CrabDude/trycatch/issues/38

[2] https://github.com/othiym23/node-continuation-local-storage

[3] https://github.com/CrabDude/trycatch/issues/37

[4] https://github.com/CrabDude/trycatch/issues/32

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

#20
post #6

q.js can properly handle exceptions thrown in a handler. see the examples here: https://github.com/kriskowal/q#chaining

So can when.js which has become my favorite promises library:

https://github.com/cujojs/when

http://know.cujojs.com/tutorials/async/mastering-async-error...

Post reply on HN