|
With Node, I often feel like I have no earthly idea in advance what a function is going to be returning. Is it going to be a promise? Is it going to be an object that isn't a promise? A string? Undefined because something got hosed up? Similarly, I find I'm often in situations where if I try to deviate slightly from a provided example -- ie, in the tutorial everything is just mashed into one large function but in my code I want to break each piece into smaller functions -- I particularly hit this snag where async fucks things up or there's some weird promise interaction or something. Is there any kind of cheat sheet or quick and dirty guide to where some common minefields are, or is this just the kind of stuff I just have to bang my head against?
|
# ¿ Aug 27, 2015 03:08 |
|
|
# ¿ May 10, 2024 08:58 |
|
piratepilates posted:For the function returning types in advance thing, check the API docs. If the API docs don't give you a good sense of it then post an angry message on their Github and choose a better library, if there isn't a better library then you're screwed. Oh, Typescript looks cool, I'll check that out more. Is it possible to have typescript and vanilla JS in the same Node app? The second thing is that, and maybe this was more when I was first learning node, I didn't always understand async and promises (and neither did anyone else on my team) so we tended to make these monstrous endpoints where the validations and all of the data massaging before calling an external API and then all of the data munging and doing what the function's real work was were all in one function in one file. Later on, when we got to the point where we had 400-line callback hell functions, we started trying to abstract out the parts that could be separated into other functions and then just call them. In Rails this was relatively[*] safe[**] to do, but in node we found that it was a good way to introduce more async-related bugs or accidentally start mixing up "callback style" and "promise style". So I guess what I'm asking for is whether or not there are a collection of Good Opinions about how to write node.js out there that can help keep teams from accidentally doing something in a weird way because they found some 2 year old article from Stack Overflow saying to do it in this one esoteric way.
|
# ¿ Aug 27, 2015 03:58 |