Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Locked thread
fantastic in plastic
Jun 15, 2007

The Socialist Workers Party's newspaper proved to be a tough sell to downtown businessmen.
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?

Adbot
ADBOT LOVES YOU

fantastic in plastic
Jun 15, 2007

The Socialist Workers Party's newspaper proved to be a tough sell to downtown businessmen.

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.

Not sure exactly what you mean by the second thing though.

edit: It may help to dig in to the internals of what the API is doing if you don't understand it. Set a breakpoint and step through it a bit and see what's in each object, maybe that'll help you figure out what they return.

edit: Oh how could I forget, just start using TypeScript. TS has definitions for types in the major NPM libraries so you'll have a much easier time figuring out what things are expecting.

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.

  • Locked thread