TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

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

45 pointsby alexgorbatchevabout 11 years ago

6 comments

ulisesrmzrocheabout 11 years ago
Generators are the best, though I&#x27;m starting to worry I&#x27;m using them too much.<p>edit: I&#x27;m using koa, so co is working in the background here. Should have said that earlier, my bad.<p>var save = function*(){ try { yield db.insertUser(); } catch (e) { throw e; } }
评论 #7599052 未加载
评论 #7598842 未加载
评论 #7598840 未加载
spankaleeabout 11 years ago
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.<p>This code, which doesn&#x27;t work as intended:<p><pre><code> function thirdPartyFunction() { function fakeXHR() { throw new Error(&quot;Invalid dependencies&quot;); } setTimeout(fakeXHR, 100); } function main() { button.on(&quot;click&quot;, function onClick() { thirdPartyFunction(); }); } main(); </code></pre> would instead look like this with a Streams-based API (I&#x27;m assuming a Dart-like API, since I don&#x27;t know the proposed Stream API for JS):<p><pre><code> function thirdPartyFunction() { function fakeXHR() { throw new Error(&quot;Invalid dependencies&quot;); } setTimeout(fakeXHR, 100); } function main() { button.on(&quot;click&quot;) .listen(function onClick() { thirdPartyFunction(); }) .onError(function onError() { console.log(&quot;now it works with errors&quot;); }) } main(); </code></pre> 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: <a href="https://github.com/whatwg/streams" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;whatwg&#x2F;streams</a><p>So Zones aren&#x27;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&#x27;s change detection code (this is why they ported Zones to JavaScript). Dart&#x27;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.
olalondeabout 11 years ago
The author does not seem like an experienced Node.js developer. All those<p><pre><code> if (err) { done(err); } else { &#x2F;&#x2F; ... } </code></pre> are usually written in this style:<p><pre><code> if (err) return done(err); &#x2F;&#x2F; ... </code></pre> I personally don&#x27;t like promises and tend to avoid libraries who use them. I don&#x27;t really see the problem they are trying to solve which is not already solvable in a more flexible way through libraries like async.
评论 #7598898 未加载
inglorabout 11 years ago
Cute, although if you promisify all your async primitives promises are throw safe and the Bluebird library makes this really easy.<p>I hope they&#x27;re able to get to as fast as Bluebird performance , which is almost as fast as callbacks and faster than the async module.
dharbinabout 11 years ago
q.js can properly handle exceptions thrown in a handler. see the examples here: <a href="https://github.com/kriskowal/q#chaining" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;kriskowal&#x2F;q#chaining</a>
评论 #7599623 未加载
eldudeabout 11 years ago
&gt; Up until recently that was pretty much all we had.<p>[Edit: see my comment below[0]. This is more a coincidence of wording than a StrongLoop marketing line]<p>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 &quot;solved this&quot; to which I made it very clear that, &quot;Yes, I did about 2 years ago with trycatch[2].&quot; Their response could be summed up as, &quot;Oh?&quot;<p>Long story short, not only does my async try&#x2F;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.<p>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.<p>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:<p>* 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<p>* Must never excecute on the same tick of the event loop<p>* Must be passed as last argument to function<p>* Return value is ignored<p>* Must not throw &#x2F; must pass resulting errors<p>* Must never be called more than once<p>Long story short, domains are broken, try&#x2F;catch is insufficient, use trycatch because I solved this problem over 2 years ago and have been perfecting it since.<p>[0] <a href="https://news.ycombinator.com/item?id=7598983" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=7598983</a><p>[1] <a href="http://www.meetup.com/BayNode/" rel="nofollow">http:&#x2F;&#x2F;www.meetup.com&#x2F;BayNode&#x2F;</a><p>[2] <a href="https://github.com/CrabDude/trycatch" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;CrabDude&#x2F;trycatch</a><p>[3] <a href="https://github.com/CrabDude/stepup" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;CrabDude&#x2F;stepup</a><p>[4] <a href="https://news.ycombinator.com/item?id=7020054" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=7020054</a><p>[5] <a href="https://gist.github.com/CrabDude/10907185" rel="nofollow">https:&#x2F;&#x2F;gist.github.com&#x2F;CrabDude&#x2F;10907185</a>
评论 #7599326 未加载
评论 #7598970 未加载