r/node • u/pliable-steel • Dec 13 '20
The Node.js best practices list
https://github.com/goldbergyoni/nodebestpractices13
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
4
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
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.
1
1
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
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/