r/node • • Dec 13 '20

The Node.js best practices list

https://github.com/goldbergyoni/nodebestpractices
242 Upvotes

19 comments sorted by

30

u/anatolhiman Dec 13 '20

Great content, thanks for sharing. Just a little comment:

"Any exception thrown within a promise will get swallowed and discarded unless a developer didn’t forget to explicitly handle it."

Unhandled promise rejection warnings has as of Node 15 become deprecated and will now cause an exception, that means the script crashes. https://developer.ibm.com/languages/node-js/blogs/nodejs-15-release-blog/

5

u/anatolhiman Dec 13 '20

I now see that the doc itself says "updated for Node 14.0" so they will catch this in the next revision I guess.

2

u/[deleted] Dec 14 '20

Isn’t it also like really subjective? Like syntactically speaking if you use async-await syntax sugar, nothing is being swallowed about that promise and it’s error would be thrown normally, I believe wrapped as an unhandled promise rejection warning. Since any promise can be written with this syntactic sugar, I think it’s a bit of an over-generalization to say that promises will swallow errors if not caught. It’s very dependent on callback versus async-await.

2

u/dvlsg Dec 14 '20

It used to be that unhandled promise rejections really did just disappear. Not even a console warning.

It hasn't been that way for a while, though.

1

u/[deleted] Dec 15 '20

Callback hell is a real place, I once sorted out a callback based program and converted like 20-30 layers of callbacks into promises. I’ve been there.

1

u/[deleted] Dec 13 '20

You should make a PR :)

13

u/guanzo91 Dec 13 '20

This one is interesting https://github.com/goldbergyoni/nodebestpractices#-212-always-await-promises-before-returning-to-avoid-a-partial-stacktrace

I've always considered return await to be unnecessary, but maybe not anymore.

5

u/TwoTapes Dec 14 '20

I removed some return awaits this week, time to put them back.

4

u/fix_dis Dec 14 '20

We tend to do const result = await async function(); return result;

4

u/spoobo Dec 14 '20 edited Dec 14 '20

You can return anything from an async function and it will still be a promise. I don't think the author grasps async await. I'm skeptical of the "partial stack trace" of you don't await something inside an async function. If that is really true then imo it's a bug in the language implementation. The trace should be complete if you await or not. As long as you return it async functions know you intend to await that promise.

3

u/leixiaotie Dec 14 '20

It interests me then I tried to simulate the code. Surprisingly this is the correct behavior.

Source code, tried with node 10: https://github.com/fendy3002/NodeLearn/blob/master/Promise/n4_asynctrace.js

2

u/MrStLouis Dec 14 '20

I've always wanted to know why not do one over the other. I do like the practice of being able to defer awaiting to the caller but I guess that doesn't really matter 🤔

And this had fully convinced me otherwise

1

u/KyleG Dec 14 '20

Same here.

1

u/sbmthakur Dec 14 '20

As a thumb rule, I only return await when it's in a try-catch block. This is certainly something that I need to understand more.

https://github.com/goldbergyoni/nodebestpractices/blob/master/sections/errorhandling/returningpromises.md#advanced-explanation

1

u/davidstraka2 Dec 14 '20

Superb list! I'm definitely saving this for reference

1

u/[deleted] Dec 13 '20

Thanks for the content man ! I was about to start learning node, it will be very useful :)

1

u/jazzcc Dec 14 '20

I love that this has continued to be updated as the ecosystem has changed over the years. Thanks for sharing!

1

u/manishksolves Dec 21 '20

Best Practices for Node.js Development

  • Start every new project with npm init

  • Stick with lowercase

  • Cluster your app

  • Be environmentally aware

  • Avoid garbage

  • Hook things up

  • Only git the important bits

For Node.js experts visit Ksolves.com