3 ms·
> Can you spot the problem with this piece of code? Sure. There's no error handling :-) (Also, if it's not some magic fetch, the return value needs to be json
by azangru 6y ago
> Can you spot the problem with this piece of code?
Sure. There's no error handling :-)
(Also, if it's not some magic fetch, the return value needs to be json-ed.)
- wereHamster 6y agoThere doesn't need to be, if you expect the caller (or the top-level code) to handle errors. Also, how should the function handle errors? It's not immediately obvious. An empty array is not the correct thing to return (an empty array could be a valid return value). Returning undefined/null would be another option, but that hides the error from the caller (which might be interested in the exact error message, to be able to properly deal with it).
- jakear 6y agoSimple! try { ... } catch (e) { throw “an unknown error occurred” } ;)
- mrspeaker 6y agoThat's what I thought they were going for at first too! "What's wrong with this code? It tries to make `async` look more readable by skipping the ugly try/catch block/s" ;)