In other words, in the example above, await proxymiseRobot.toggle() will return null, instead of undefined.
This try/catch mechanism is great when we got an error and should be kept, but would be nice to have a third case for undefined like:
if (d.trim() == "undefined") {
cb(undefined)
} else {
// do what is already being done
}
but you might be able to make get return a function, but with a valueOf method
Do you mean something like this?
var a = function() { return 12;}
a.prototype.valueOf = function() { return 42;};
console.log(a) // would like 42, got function() { return 12;}
console.log(a()) // would like 12, got 12
Would be perfect indeed, but does not seem to work for functions.
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.
Thanks fo the feedback!
Yes!
for (a in global) { console.log(a, "is a", typeof eval(a)) }
. I will have a think if it makes sense to use this instead.I meant
UART.eval('functionwithnoreturnstatement()', cb)
, will try to evaluate the result of the function call on the device, getundefined
back, then try to interpret is as a JSON (https://github.com/espruino/EspruinoWebTools/blob/13032d0e862d9976ed5eed33b42f07e93e6c25c3/uart.js#L492). It will fail, be send to the catch, and call the callback withnull
(https://github.com/espruino/EspruinoWebTools/blob/13032d0e862d9976ed5eed33b42f07e93e6c25c3/uart.js#L496). So we cannot make the difference between a successful call to a real function and a call to something that does not exist or failed to execute properly.In other words, in the example above,
await proxymiseRobot.toggle()
will returnnull
, instead ofundefined
.This try/catch mechanism is great when we got an error and should be kept, but would be nice to have a third case for
undefined
like:Would be perfect indeed, but does not seem to work for functions.