Unexceptional
Exception safe functions and promises by converting thrown errors to values (like Go and Lua). See this gist for background.
NOTE: This meant initially developed for usage together with async/await
and function*/yield
, so all results are converted to promises (see the source in index.js
for more info).
Usage examples
Safe(r) functions:
const safeFunction = ; { if shouldThrow throw 'Something went wrong'; else return val; } const iDontThrowAnyMore = ; { const error result = await ; // error === undefined // result === 50 if error // handle error as a value or ignore const error2 result2 = await ; // error2 === Error('Something went wrong'); // result2 === undefined if error2 // handle error as a value or ignore } ;
Safe(r) promises:
const safePromise = ; { return { if shouldReject ; else ; };} { const error result = await ; // error === undefined // result === 50 if error // handle error as a value or ignore const error2 result2 = await ; // error2 === Error('Something went wrong'); // result2 === undefined if error2 // handle error as a value or ignore }