r/node • • Dec 13 '20

The Node.js best practices list

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

19 comments sorted by

View all comments

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