I recently looked at my code and realised that my core logic was diluted with error handling code. After calling a function, I want to be sure that whatever happens inside it worked as expected, so I always add the line “if (!res.success) return res” after each call. I know this can be avoided by throwing errors, but I want errors to be the last resort and only be thrown if a case occurred that was not handled correctly by the program.
So I thought it might be a good idea to have something like a special return keyword that checks the result and then only returns if the check fails .
I would like to suggest a kind of “mayReturn” keyword that has a built-in check and may return the current function's scope if a condition is met. It's difficult to come up with a name for this keyword, but it could be called “gate”. This would allow one to “gate” the resu lt.
When should the context be returned?
- undefined or null is returned.
- false is returned
- An object with a success key is returned, which is false.
- An object with an OK key is returned and it is false.
Examples:
Current:
let writeResult = await writeData(nodeId)
if (!writeResult.success) return writeResult
With the new "gate" keyword:
gate writeData(nodeId)
Or, in the case where the result is needed:
Current:
let configResult = await getConfig(nodeId)
if (!configResul t.success) return configResult
console.log(configResult)
With the new keyword:
let configResult = gate await writeData(nodeId)
console.log(configResult)
(It would be nice if one could just write gate, not gate and await, but I am not sure if this is possible.)
Let me know if this idea is worth pursuing. If someone wants to mentor the proposal process, feel free to reach out.
Thanks for reading this, and I hope that there is no obvious way to write this the same compact way out there t hat I am mis sing . If so, I a pologise in advance.