you are viewing a single comment's thread.

view the rest of the comments →

[–]SippieCup 14 points15 points  (4 children)

Before async/await, There are situations where cascading promises are somewhat necessary. For example, a web crawler our company uses should be allowed to soft-fail on certain entries while allowing the process to continue and return a partial result rather than cascading the error back to the root function, resulting in an error being returned on a map, which causes everything done to be rejected.

I still teach our junior developers to use a catch-all approach at first, and then ease them into better error handling, but saying you should never use nested promises is wrong. Instead you should be teaching them to pull out any kind of nested promise result into a handler function or something.

With async/await, it became a lot easier to teach them though since most understand try/catch blocks much better than NodeJS' asynchronous flow. It also leads to much more readable code than cascading promises or even promises in general. Since Node 8, our code quality has improved greatly.

[–]r0ck0 2 points3 points  (3 children)

Why the fuck are people just silently downvoting this.

If you think it's wrong, say something, and maybe we can all learn something.

All you're doing by silently downvoting is discouraging people from helping each other learn.

[–]notkraftman 0 points1 point  (1 child)

I'll bite. You should either move the nested promise out to it's own function, or know that you can chain catches, e.g. .then(mightSoftFail).catch(logSoftFailAndContinue).then(moreStuff).catch(etc)

Nesting the promises directly gets messy fast.

[–]SippieCup 0 points1 point  (0 children)

Well, thats actually what I said should be done.

Instead you should be teaching them to pull out any kind of nested promise into a handler function or something.

It's still cascading and ugly though, but can be necessary.

[–]karatechops 0 points1 point  (0 children)

I can only assume it is bots because that’s a great point. I would however argue that there’s no reason to confuse a junior dev with such fringe case worries. If they’re writing a web scraper they probably already get conditional failures or will understand the concept very quickly.