-
-
Save eiriklv/1fb4af8a268b394ddce5b8e4623bc546 to your computer and use it in GitHub Desktop.
/** | |
* 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) | |
*/ | |
} | |
//... | |
} |
Why not use an object?
function safePromise (promise) {
return promise
.then(result => { result })
.catch(error => { error })
}
// ...
const { error, result } = await safePromise(getAsset(10))
Arrays signify order, but this way you can neatly ignore the error, as opposed to doing something like const [ , result]
.
const { result } = await safePromise(getAsset(10))
@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
and result
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 called error
, the result is called result
)
Consider the following comparison:
// array / tuple
const [firstError, firstResult] = await safePromise(getAsset(10));
const [secondError, secondResult] = await safePromise(getAsset(20));
const [,thirdResult] = await safePromise(getAsset(30));
const [,fourthResult] = await safePromise(getAsset(40));
// vs.
// object / named parameters
const { error: firstError, result: firstResult } = await safePromise(getAsset(10));
const { error: secondError, result: secondResult } = await safePromise(getAsset(20));
const { result: thirdResult } = await safePromise(getAsset(30));
const { result: fourthResult } = await safePromise(getAsset(40));
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 ✌️
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 ✌️️
Makes sense!