Last active
February 2, 2019 12:13
-
-
Save eiriklv/1fb4af8a268b394ddce5b8e4623bc546 to your computer and use it in GitHub Desktop.
Exception free JavaScript?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* WHY? - BECAUSE EXCEPTIONS/TRY/CATCH IS A GLOBAL HORRIBLE MESS :-( | |
* Check out error handling in golang: https://blog.golang.org/error-handling-and-go | |
*/ | |
/** | |
* Wrap an "unsafe" promise | |
*/ | |
function safePromise(promise) { | |
return promise | |
.then(result => [undefined, result]) | |
.catch(error => [error, undefined]); | |
} | |
/** | |
* Wrap an "unsafe" function that might throw | |
* upon execution in a function that returns | |
* a promise (which is handled "safely" with safePromise) | |
* | |
* NOTE: This will only handle throws that | |
* are done within the same execution tick, | |
* and not errors that are thrown "later" | |
* within the same context (no way to do that..) | |
*/ | |
function safeFunction(fn) { | |
return function(...args) { | |
let error = undefined; | |
let result = undefined; | |
try { | |
result = fn.apply(this, args); | |
} catch (e) { | |
error = e; | |
} | |
return safePromise(error ? Promise.reject(error) : Promise.resolve(result)); | |
} | |
} | |
/** | |
* Example of promise returning function that rejects | |
*/ | |
function getAsset(id) { | |
return Promise.reject(new Error('Booo!')); | |
} | |
/** | |
* Example use with async/await | |
*/ | |
async function letsDoThis() { | |
// Alt 1 (wrapping a promise returning function) | |
const [error, result] = await safeFunction(getAsset)(10); | |
// Alt 2 (wrapping a promise) | |
const [error, result] = await safePromise(getAsset(10)); | |
if (error) { | |
/** | |
* Handle the error appropriately | |
* (You could of course just throw it here if you wanted to - but it is at least optional) | |
*/ | |
} | |
//... | |
} |
Shameless plug, I have a fork of co.js exactly for this purpose https://github.com/dg3feiko/co-nothrow
Line 52, and, Line 54 seems declare error
twice?
If someone working on Node.js that concerns about performance trade-off when using try-catch then V8 recently applied turbofan optimization which is considered fixed the trade-off.
@tiansh - I'm just showing two ways of achieving the same thing ✌️️
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@sscotth - You could use an object like that, but that comes with its own set of drawbacks. With an object you have to be explicit about the name of the
error
andresult
property (or whatever you choose to call it - and naming things is hard). With the array version you're really just emulating a tuple (an ordered set of values) as a way of having multiple return values, which JavaScript doesn't have as a native data structure (as opposed to golang, which does).const [,result] = ...
might be awkward, but it comes with simpler conventions / less decisions (the error comes first, the result comes second) as opposed to the object with properties (the error is callederror
, the result is calledresult
)Consider the following comparison:
It comes down to taste i guess, but in general I tend to prefer the solution that introduces the least amount of decisions to be made
Edit: Also - the array/tuple version forces you to be explicit about ignoring the error, which imo is a good thing ✌️