[conspire] Dependency management: solved

Ron admin at bclug.ca
Sun Apr 27 03:53:50 PDT 2025


Steve Litt wrote on 2025-04-26 17:13:

> Can somebody please explain the mindset of Node.Js to me?

It's a tool for running JS in a CLI environment.

Basically the JS engine (v8?) from Chromium browser.


> Every time I've tried it, after going 4 or 5 callbacks deep I felt 
> like I was trying to turn a 50 foot garden hose inside out.

I like that analogy.


Callbacks are a feature of *JavaScript* and many other languages - isn't
it quite similar to functional programming?

https://en.wikipedia.org/wiki/Callback_%28computer_programming%29#Example_code


Sometimes, having difficulty managing such complexity indicates time to 
move to a different tool set.

i.e. 50 files in a project is getting too many for nano, time for ...

This is why variations on VS Code / Codium are popular - that's a really 
good way to manage many complex features across many files for many 
languages.



> What is it that I'm not getting?

Once one gets used to them, they're really powerful, especially with
asynchronous code.

Passing a function as a parameter to a function is really natural and
one soon asks oneself, "How did I do it when this could not be done?"


i.e. the setTimeout() function - after one second, run something:


How would this work without passing a callback (function)?


function myFunc() {...}

setTimeout( myFunc, 1000)

Now one has a function called myFunc that is entirely useless outside 
the scope of giving the setTimeout something to run, so is kind of 
polluting the namespace of the app.

However:

setTimeout( () => {...}, 1000)

creates no unnecessary unnamed functions by passing in an anonymous 
callback function.

It's cleaner than having myFunc completely separate, or having it in 
this form:

setTimeout( function myFunc() {console.log("callback!")}, 1000)



(Yes, this is touching on callbacks and anonymous functions, but they're 
peanut butter & chocolate in JS).


So, setTimeout wants a *function* to run, not a code block, so we give 
it one, and therefore it's a callback (a function passed to a function).




Kind of like regular expressions - once comfortable with them it'd be
painful to go without them.


Upon first encounter, anonymous (arrow notation) functions, like 
lambdas,  are a giant WTF, but eventually you'll be using them everywhere.

It's just easier to write and read.


i.e.

function myFunc() {...}
vs
const myFunc = () => {...}


and in the setTimeout() above.




Closure is similar: a function that returns a function (outer function
gets called, returns inner function, which has access to variables
defined in outer function).

It's all really weird and nonsense until... one is trying to solve a
problem and suddenly, the answer is closure!


It's happened to me, and I really wish I could think of an example off
the top of my head.

It would have been an enormous headache to write it without such tricks.


JS has lots of them, and they're often pretty cool.



More information about the conspire mailing list