Without being to familiar with the detail - and going a bit on a limb - I try to answer the difference between
returning cb; vs return cb(d);
From variable name, I assume, it is a callback. It is for sure a function, because otherwise cb(d) would have given you an error.
In JavaScript everything is an object... and a function, like cb, is also an object and can be passed around by reference as argument, assigned to a variable, returned as an object, etc. In the first case - return cb; - function object is returned and something else will invoke it... just as you would hand over an Icemaker vs. handing over Ice it has made, so someone else can push the button and make it make ice at their time and convenience.
JavaScript syntax defines that a function reference (function name) followed by opening and closing parenthesis it to be invoked. In other words, cb(d) invokes the callback function with passing the d(ata) argument. With the return, you return what the callback function returns... which usually is undefined, because there is nothing around that could 'catch' what it returns.
Typical with callbacks is that their invocation has to be deferred, because the parameter(s) that have to be passed on invocation are not available yet with the right values, and therefore the callback is handed to the next processing stage, to finally end up at the point AND time where the arguments are ready / populated with the right values and the callback can be invoked.
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
Without being to familiar with the detail - and going a bit on a limb - I try to answer the difference between
From variable name, I assume, it is a callback. It is for sure a function, because otherwise
cb(d)
would have given you an error.In JavaScript everything is an object... and a function, like
cb
, is also an object and can be passed around by reference as argument, assigned to a variable, returned as an object, etc. In the first case -return cb;
- function object is returned and something else will invoke it... just as you would hand over an Icemaker vs. handing over Ice it has made, so someone else can push the button and make it make ice at their time and convenience.JavaScript syntax defines that a function reference (function name) followed by opening and closing parenthesis it to be invoked. In other words,
cb(d)
invokes the callback function with passing thed
(ata) argument. With the return, you return what the callback function returns... which usually is undefined, because there is nothing around that could 'catch' what it returns.Typical with callbacks is that their invocation has to be deferred, because the parameter(s) that have to be passed on invocation are not available yet with the right values, and therefore the callback is handed to the next processing stage, to finally end up at the point AND time where the arguments are ready / populated with the right values and the callback can be invoked.