Struggling to grasp callbacks.

They're simple.

The reason you're confused is because JavaScript lacks explicitly written types and you don't know closures and variable life cycles.

Think of it this way, when a function that takes a callback is invoked/called then that entire callback/function definition now lives within the called function.

I.e.

function derp() { return 1; }

function horse(cb) {

return 1 + cb() ; }

function horse2() { function cb() { return 1; } return 1 + cb() ; }

If you see the example above, in the first scenario you have function horse that accepts a callback which makes it more versatile and in horse2 you see the same cb function is defined internally but you have the same result.

Hopefully this helps.

/r/node Thread